[R] Enabling s3 methods by attaching environment

Valentin Petzel v@|ent|n @end|ng |rom petze|@@t
Tue Dec 13 17:58:02 CET 2022


Hello everyone!

if we define some S3 method in some environment like this

E <- new.env() 
evalq({ 
  test <- function(x) UseMethod("test") 
  test.character <- function(x) 1 
}, envir = E)

this works

evalq(test("a"), envir = E) 
but this doesn’t:

attach(E) 
test("a")
According to some remarks this now requires the use of .S3method to register the 
method to a method data base. This does work, but if we create two different 
environments with different implementations of the same S3 method this will always 
default to the last defined method (as the method data base is only updated on 
declaration).

This makes a workflow where we use environments to enable and disable certain functions 
impossible.

So I want to ask if there is currently any way to use S3 methods in a currently attached 
environment while allowing to switch the method by attaching a different environment?

Kind Regards,
Valentin

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part.
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20221213/1bc984b5/attachment.sig>


More information about the R-help mailing list