Skip to content
Prev 40268 / 63421 Next

By default, `names<-` alters S4 objects

On 11-05-15 11:33 AM, John Chambers wrote:
Sure!

   https://stat.ethz.ch/pipermail/r-devel/2009-March/052386.html
Ah, that's interesting. I didn't know I could put a names slot in my
class. Last time I tried was at least 3 years ago and that was causing
problems (don't remember the exact details) so I ended up using NAMES
instead. Trying again with R-2.14:

   > setClass("A", representation(names="character"))

   > a <- new("A")

   > attributes(a)
   $names
   character(0)

   $class
   [1] "A"
   attr(,"package")
   [1] ".GlobalEnv"

   > names(a)
   NULL

   > names(a) <- "K"

   > attributes(a)
   $names
   [1] "K"

   $class
   [1] "A"
   attr(,"package")
   [1] ".GlobalEnv"

   > names(a)
   NULL

Surprise! But that's another story...
Or, at a minimum (if no consensus can be reached about the above
strategy), not add a "names" attribute set to NULL. My original
post was more about keeping the internal representation of objects
"normalized", in general, so identical() is more likely to be
meaningful.

Thanks,
H.