[Rd] attributes of S4 objects

From: Parlamis Franklin <fparlamis_at_mac.com>
Date: Thu 14 Sep 2006 - 06:07:53 GMT


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



R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Thu Sep 14 16:09:45 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 15 Sep 2006 - 14:30:07 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.