Which R objects can have arbitrary attributes?

Viewed 92

Hadley Wickham writes in Advanced R (http://adv-r.had.co.nz/Data-structures.html):

All objects can have arbitrary additional attributes, used to store metadata about the object.

But what about the following:

> a <- as.symbol("a")
> attr(a, "attribute")
NULL
> attr(a, "attribute") <- "test"
Error in attr(a, "attribute") <- "test" : 
  cannot set attribute on a symbol

In the R documentation, the only thing I could find is that NULL cannot have attributes "as there is only one null object."

So what are the general rules that apply here?

1 Answers

From R 3.5.0 (see changelog on CRAN):

Attributes on symbols are now detected and prevented; attempt to add an attribute to a symbol results in an error.

I am still on R 3.4.4. Your code works fine for me.

So previously, NULL was the only thing that can not have attributes; now there is another: "name" / "symbol".

Related