[Rd] attributes of S4 objects

Parlamis Franklin fparlamis at mac.com
Thu Sep 14 08:07:53 CEST 2006


I am having a bit of a struggle deciding when to use attributes with  
S4 objects.  Clearly the 'class' attribute will always be present.   
But it is not clear to me whether the architects of the methods  
package intend that other attributes, such as 'names', will still be  
used when the S4 implementation is complete.

I notice that, when you create a formal object from an informal one  
with attributes, the attributes are (often) assigned to the formal  
object.  For example,

setClass("Numeric", representation("numeric"))
vec <- 1:10
names(vec) <- letters[1:10]
comment(vec) <- "comment"
Vec <- new("Numeric", vec)
attributes(Vec) ## 'names' and 'comment' attributes are assigned to  
the formal object

But I also notice that, in the absence of an appropriate 'coerce'  
method, 'as(   , superclass)' will return an object without attributes

as(Vec, "numeric") ## gives an unnamed vector of mode numeric with no  
comment attribute

Because of this, I have found myself writing methods for the sole  
purpose of preserving attributes when coercing between basic data  
types and formal classes that extend them.  But the default methods  
for coercing to the basic data types clearly want attributes to be  
stripped (they do so explicitly when strict=TRUE (the default)).  I  
am thinking that maybe it was always intended that non-class  
attributes would not be used with formal objects, and that instead  
analogous slots would appear in any formal objects that extend basic  
data types (like the Dim and Dimnames attributes in the 'Matrix'  
mother class from the Matrix package).

Is that true?  Are attributes un-S4?  Any and all style advice or  
examples would be appreciated.

Franklin Parlamis




More information about the R-devel mailing list