[Rd] S4 class redefinition

Ross Boylan ross at biostat.ucsf.edu
Tue Jun 30 21:58:32 CEST 2009


I haven't found much on S4 class redefinition; the little I've seen
indicates the following is to be expected:
1. setClass("foo", ....)
2. create objects of class foo.
3. execute the same setClass("foo", ...) again (source the same file).
4. objects from step 2 are now NULL.

Is that the expected behavior (I ran under R 2.7.1)?

Assuming it is, it's kind of unfortunate.  I can wrap my setClass code
like this
if (! isClass("foo")) setClass("foo", ....)
to avoid this problem.  I've seen this in other code; is that the
standard solution? 

I thought that loading a library was about the same as executing the
code in its source files (assuming R only code).  But if this were true
my saved objects would be nulled out each time I loaded the
corresponding library.  That does not seem to be the case.  Can anyone
explain that?

Do I need to put any protections around setMethod so that it doesn't run
if the method is already defined?

At the moment I'm not changing the class defintion but am changing the
methods, so I can simply avoid running setClass.

But if I want to change the class, most likely by adding a slot, what do
I do?  At the moment it looks as if I'd need to make a new class name,
define some coerce methods, and then locate and change the relevant
instances.  Is there a better way?

Thanks.
Ross Boylan



More information about the R-devel mailing list