[Rd] inconsistency in attaching attributes to NULL

William Dunlap wdunlap at tibco.com
Sat Sep 19 00:21:53 CEST 2009


In R-devel (svn 49628) and back to at least R 2.7.0 we get
inconsistent results when attempting to attach attributes to
a variable with the value NULL.

If we use attributes<- it finishes but changes the value to list().

   > a<-NULL
   > attributes(a)<-list(attr1="First attribute", attr2=2+2i)
   > a
   list()
   attr(,"attr1")
   [1] "First attribute"
   attr(,"attr2")
   [1] 2+2i

If we use attr<- it refuses to attach the attribute

   > b<-NULL
   > attr(b, "attr1") <- "First attribute"
   Error in attr(b, "attr1") <- "First attribute" :
     attempt to set an attribute on NULL

Is the second behavior the correct one and should attributes(x)<-list()
abort if x is NULL?

(attributes(NULL)<-list(attr="First") dies because NULL itself is not
allowed on the left side of an assignment.)

Bill Dunlap
TIBCO Software Inc - Spotfire Division
wdunlap tibco.com 



More information about the R-devel mailing list