[Rd] Inheriting from "environment" and similar object types

John Chambers jmc at r-project.org
Tue Feb 17 00:54:28 CET 2009

Attributes can't be assigned to objects of type "environment" or similar 
types such as external pointers or names (symbols).  The objects are 
references, not normal R objects, and are not copied by the internal 
duplicate() routine, so any attribute (including "class") overwrites the 
same object.  This means that classes, either S4 or S3, can't inherit 
from these types directly.

A mechanism has been added to r-devel (version r47933) that allows S4 
classes to contain "environment" and similar types.  The mechanism uses 
a reserved slot name to hold the reference.  Code in various places 
recognizes S4 objects with this slot and coerces the object to the 
corresponding type. See ?setClass.

The mechanism is transparent for quite a few computations, but there 
can't be a full guarantee, since low-level code can operate directly 
using the object type, which of course will not correspond to 
"environment".  Usually, a workaround is to pass in as(object, 
"environment") instead of object.

The same mechanism is used to allow extending  "externalptr" and "name", 
and other similar types will likely be added after some more testing.

Details of the mechanism are still experimental,  and may change.


More information about the R-devel mailing list