[Rd] S4 creation where= problems

Martin Morgan mtmorgan at fhcrc.org
Wed Aug 16 18:04:16 CEST 2006

I had this problem creating S4 objects with 'where'

> env <- new.env()
> setClass("A", where=env)
[1] "A"
> setClass("A", representation(x="numeric"), where=env)
[1] "A"
> setClass("A", representation("numeric"), where=env)
Error in exists(x, envir, mode, inherits) : 
	invalid first argument

John Chambers (below) suggests I bring this to R-devel. My primary use
is to minimize inadvertent consequences of class creation, e.g., by
overwriting an existing (user) class definition during unit testing or
in .Rd examples. There are also programming opportunities from adding a
suite of class definitions, etc., to an environment, and then
'attach'ing and removing or otherwise exposing the environment in a
modular way.


John Chambers <jmc at r-project.org> writes:

> Hmm, an interesting one!  There's a good deal of code that assumes
> classes and methods are defined in a _package_, even though
> technically it's allowed for where= to be an ordinary environment. 
> What's happening here is that the package name is empty, eventually
> resulting in a problem.

> Class definitions, generic functions and some other objects are
> constructed with a "package" slot, assumed to be a non-empty
> character string.  That's the mechanism by which R recognizes the
> corresponding package reference when the class, function, etc is
> being used during a later session.  It's possible to patch over the
> specific issue here quite easily (it has to do with creating a
> method for as(x, "numeric") when x is from class "A"), but it's far
> from clear that the result will work in a general sense, when the
> environment is used later on, say.

> If you have a good reason for wanting to use non-package
> environments this way, it's something we should think about & deal
> with cleanly.  It would be good, if so, to start a discussion on
> r-devel.

More information about the R-devel mailing list