inheritance and attach

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

inheritance and attach

R devel mailing list
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
[hidden email]

"TERR-ree THUR-noh"


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: inheritance and attach

Simon Urbanek
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 <[hidden email]> 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
> [hidden email]
>
> "TERR-ree THUR-noh"
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: [EXTERNAL] Re: inheritance and attach

R devel mailing list
Thanks Simon.  I missed that.   It is a sensible change.
I had trouble because I had just changed computing environments this weekend (a forced
change due to an institutional directive), and this caught me right after that so I spent
some time chasing my tail.  Murphy's law...

Terry T.


On 3/15/21 4:45 PM, Simon Urbanek wrote:

> 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 <[hidden email]> 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
>> [hidden email]
>>
>> "TERR-ree THUR-noh"
>>
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel