[ESS] ess-developer

Vitalie Spinu spinuvit at gmail.com
Tue Apr 23 20:57:34 CEST 2013


I got down to this, and to my own surprise this behavior is by my own
design:) I just forgot it. This is the comment I have in
/etc/ess-developer.R:
   
   
   ## COMMENT ON S3 METHODS: It is not feasible and, quite frankly, a bad practice
   ## to check all the assigned function names for "." separator. Thus, S3 methods
   ## are not automatically registered. You can register them manually with
   ##
   ##    registerS3method("method_name", "my_class", my_package:::method_name.my_class)
   ##
   ## after you have inserted method_name.my_class into your package environment
   ## using ess-developer. Otherwise, R will call the registered (i.e. cached) S3
   ## method instead of the new method residing in the package environment.


ESS-developer handles correctly s3 methods registered locally in your
own package, but it doesn't look for methods registered in other
packages (like AIC in your example).

So for time being you have two options. First one (recommended) use S4
setMethod function. In this case ess-tracebug will do the right thing on
the fly. Second, use registerS3method as recommended in the comment
above.

If someone knows a smart inbuilt way to check if foo.bar is a S3 "foo"
method for class "bar" then please let me know. Otherwise I will move
the above comment into the official documentation. 

Another reason for me not to spend to much time on this is that I would
not like ess-developer to propagate the use of S3 for new projects:)

    Vitalie



 >> Kevin Wright <kw.stat at gmail.com>
 >> on Sun, 21 Apr 2013 09:37:18 -0500 wrote:

 > First, is there a function like 'methods', but which also shows the package
 > for each function?

 > Now, to your question.  Here is an example that is not quite the same as
 > what I had earlier, but exhibits some similar behavior.  Let me know if I'm
 > doing something wrong.

 > Say I want to change AIC to print "Akaike" instead of "AIC".

 > Here is the new function.

 > AIC.default <- function (object, ..., k = 2)
 > {
 >     ll <- if ("stats4" %in% loadedNamespaces())
 >         stats4:::logLik
 >     else logLik
 >     if (!missing(...)) {
 >         lls <- lapply(list(object, ...), ll)
 >         vals <- sapply(lls, function(el) {
 >             no <- attr(el, "nobs")
 >             c(as.numeric(el), attr(el, "df"), if (is.null(no)) NA_integer_
 > else no)
 >         })
 >         val <- data.frame(df = vals[2L, ], ll = vals[1L, ])
 >         nos <- na.omit(vals[3L, ])
 >         if (length(nos) && any(nos != nos[1L]))
 >             warning("models are not all fitted to the same number of
 > observations")
 >         val <- data.frame(df = val$df, Akaike = -2 * val$ll + k *
 >             val$df)
 >         Call <- match.call()
 >         Call$k <- NULL
 >         row.names(val) <- as.character(Call[-1L])
 >         val
 >     }
 >     else {
 >         lls <- ll(object)
 >         -2 * as.numeric(lls) + k * attr(lls, "df")
 >     }
 > }

 > Here is my session transcript, with comments to show ESS actions.

 > R version 3.0.0 (2013-04-03)
 > Done with c:/kw/.Rprofile.
 R> R> options(chmhelp=FALSE, help_type="text")
 R> options(STERM='iESS', str.dendrogram.last="'", editor='emacsclient.exe',
 > show.error.locations=TRUE)
 R> 
 R> lm1 <- lm(Fertility ~ . , data = swiss)
 R> lm2 <- update(lm1, . ~ . -Examination)
 R> AIC(lm1, lm2)
 >     df      AIC
 > lm1  7 326.0716
 > lm2  6 325.2408
 > # ESS: developer active, developing stats
 R> library('stats')
 R> methods('AIC')
 > [1] AIC.default* AIC.logLik*

 >    Non-visible functions are asterisked
 R> getAnywhere('AIC.default')
 > A single object matching 'AIC.default' was found
 > It was found in the following places
 >   registered S3 method for AIC from namespace stats
 >   namespace:stats
 > with value

 > function (object, ..., k = 2)
 > {
 >     ll <- if ("stats4" %in% loadedNamespaces())
 >         stats4:::logLik
 >     else logLik
 >     if (!missing(...)) {
 >         lls <- lapply(list(object, ...), ll)
 >         vals <- sapply(lls, function(el) {
 >             no <- attr(el, "nobs")
 >             c(as.numeric(el), attr(el, "df"), if (is.null(no)) NA_integer_
 > else no)
 >         })
 >         val <- data.frame(df = vals[2L, ], ll = vals[1L, ])
 >         nos <- na.omit(vals[3L, ])
 >         if (length(nos) && any(nos != nos[1L]))
 >             warning("models are not all fitted to the same number of
 > observations")
 >         val <- data.frame(df = val$df, AIC = -2 * val$ll + k *
 >             val$df)
 >         Call <- match.call()
 >         Call$k <- NULL
 >         row.names(val) <- as.character(Call[-1L])
 >         val
 >     }
 >     else {
 >         lls <- ll(object)
 >         -2 * as.numeric(lls) + k * attr(lls, "df")
 >     }
 > }
 > <bytecode: 0x066612dc>
 > <environment: namespace:stats>
 R> # ESS: source new function C-c C-f
 R> getAnywhere('AIC.default')
 > A single object matching 'AIC.default' was found
 > It was found in the following places
 >   registered S3 method for AIC from namespace stats
 >   namespace:stats
 > with value

 > <srcref: file "c:/x/bug.r at 1" chars 1:16 to 27:1>
 > <environment: namespace:stats>
 > Warning message:
 > In file(srcfile$filename, open = "rt", encoding = encoding) :
 >   cannot open file 'c:/x/bug.r at 1': No such file or directory
 R> 

 > On Sun, Apr 21, 2013 at 9:01 AM, Vitalie Spinu <spinuvit at gmail.com> wrote:

 >> 
 >> Kevin,
 >> 
 >> Is there a way to reproduce your problem with one of the S3
 >> methods/generics in default R packages?
 >> 
 >> I am not using S3 myself and, quite frankly, I have already forgotten
 >> the exact caching mechanism of S3 methods. Also this mechanism might
 >> have been changed in 3.0.0, so a reproducible example would be very much
 >> welcome.
 >> 
 >> Thanks,
 >> Vitalie
 >> 
 >> 
 >> 
 >> >> Vitalie Spinu <spinuvit at gmail.com>
 >> >> on Sun, 21 Apr 2013 14:01:55 +0200 wrote:
 >> 
 >> >> Martin Maechler <maechler at stat.math.ethz.ch>
 >> >> on Sat, 20 Apr 2013 18:02:29 +0200 wrote:
 >> 
 >> >> On Sat, Apr 20, 2013 at 1:38 AM, Vitalie Spinu <spinuvit at gmail.com>
 >> wrote:
 >> >>
 >> >> As long as the two copies are the same, it should not matter for
 >> >> anything. Why does that bother you specifically?
 >> >>
 >> 
 >> >> Well, I think it should be bothering a bit.  It invalidates the
 >> principle
 >> >> that using ESS, from the point of view of R, is as much as possible
 >> like
 >> >> using R outside ESS  .. only better, of course.
 >> 
 >> > This is how R works. It keeps several copies of a function on some
 >> > occasions. S3 is one of those.
 >> 
 >> >     Vitalie
 >>



More information about the ESS-help mailing list