[Rd] inconsistency in attaching attributes to NULL

cls59 chuck at sharpsteen.net
Sat Sep 19 00:32:56 CEST 2009




William Dunlap wrote:
> 
> 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
> 
> 


It's probably an unfortunate inconsistency that you are able to set
attributes on a using attributes() <- when a is NULL. According to the R
Language Definition (Section 2.1.6: NULL):


R Language Definition wrote:
> 
> The NULL object has no type and no modifiable properties. There is only
> one NULL object in R, to which all instances refer. To test for NULL use
> is.null. You cannot set attributes on NULL. 
> 

Looks like there's a missing error message that should be there :P

-Charlie

-----
Charlie Sharpsteen
Undergraduate
Environmental Resources Engineering
Humboldt State University
-- 
View this message in context: http://www.nabble.com/inconsistency-in-attaching-attributes-to-NULL-tp25516381p25516472.html
Sent from the R devel mailing list archive at Nabble.com.



More information about the R-devel mailing list