[Rd] inheritance and attach

Simon Urbanek @|mon@urb@nek @end|ng |rom R-project@org
Mon Mar 15 22:45:31 CET 2021


Terry,

NEWS: CHANGES IN R 4.0.0 NEW FEATURES

     \item S3 method lookup now by default skips the elements of the
      search path between the global and base environments.

If you use attach(), S3 methods are hence no longer dispatched to (because it is between global and base) unless you register them using .S3method(). Without registration you have to load them into the global env for them to work since this is now the only environment that doesn't require registration.

Cheers,
Simon



> On Mar 16, 2021, at 7:19 AM, Therneau, Terry M., Ph.D. via R-devel <r-devel using r-project.org> wrote:
> 
> This change in R-devel just bit me.   Under the newest release, if I attach() another 
> .RData directory, the methods are not detected.
> Was it intentional?   Running in Linux.   Here is a script of an example that works fine 
> under 3.6.2. but fails in R-devel.
> 
> tmt% mkdir temp1
> tmt% cd temp1
> tmt% R
>  # define a silly method, just for testing
> 
> charlie <- function(x, ...)
>     UseMethod("charlie")
> 
> 
> charlie.default <- function(x, ...) {
>     cat("default method ", x, "\n")
>     x +2
> }
> 
> charlie.character <- function(x, ...) {
>     cat("character method ", x, "\n")
>     as.character(as.numeric(x) + 2)
> }
> 
>> quit("yes")
> 
> tmt% cd ..
> tmt% R
>> attach("temp1/.RData")
>> charlie( 4)
> Error in UseMethod("charlie") :
>   no applicable method for 'charlie' applied to an object of class "c('double', 'numeric')"
> 
> ----------------------------------------
> 
> The use case was my local test environment for the survival package.  I can work around it.
> 
> -- 
> Terry M Therneau, PhD
> Department of Health Science Research
> Mayo Clinic
> therneau using mayo.edu
> 
> "TERR-ree THUR-noh"
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 



More information about the R-devel mailing list