[R] S4 class slot name 'names' is not allowed

Steven Lacey slacey at umich.edu
Thu Apr 13 13:34:04 CEST 2006


Thanks! I'll use name for now.


-----Original Message-----
From: Martin Maechler [mailto:maechler at stat.math.ethz.ch] 
Sent: Thursday, April 13, 2006 3:11 AM
To: Steven Lacey
Cc: R-bugs at stat.math.ethz.ch; r-help at stat.math.ethz.ch
Subject: S4 class slot name 'names' is not allowed

>>>>> "SteveL" == Steven Lacey <slacey at umich.edu>
>>>>>     on Wed, 12 Apr 2006 19:06:52 -0400 writes:

    SteveL> Hi, Why doesn't this work?
    setClass("tests", representation(names = "character"))
    tmp <- new("tests"); tmp at names <- "a"
    SteveL> Error in "slot<-"(object, name, TRUE, value) :
    SteveL> 'names' attribute [1] must be the same length as the vector [0]
    SteveL> Why does this work (replace names with name)?
    setClass("test", representation(name="character"))
    tmp <- new("test"); tmp at name <- "a"
    SteveL> I looked at the help for setClass, representation,
    SteveL> and slot and found no restriction on the slot names
    SteveL> other than that they "be any non-empty string, but
    SteveL> if the name is not made up of letters, numbers, and
    SteveL> '"."', it needs to be quoted".

Hah, you got us!
This *is* a bug - at least in the documentation  ===> This goes to R-bugs as

It's because of the current *implementation* of S4-classed objects in R [as
an empty list with a list of unusual attributes]
-- and because  'names'  (and also, e.g., 'dimnames')
are attributes in R with a very special treatment - in many places.

I haven't delved into the internal code, but I know that this problem will
go away ``as soon as'' the internal representation of S4-classed objects is
replaced -- something that has been planned for a while and "just did not
yet happen".

Martin Maechler, ETH Zurich

More information about the R-help mailing list