I am reading an Advanced R topic on data structures and attributes. It says:
You should always get and set these attributes with their accessor func
You shouldn't access attributes directly because the author of code should provide an API for you to use to access them. That gives them the flexibility to change the underlying code without changing the API.
The xts package provides a good example of this:
> library(xts)
> x <- xts(1:3, as.POSIXct("2014-01-01")+0:2)
> index(x)
[1] "2014-01-01 00:00:00 CST" "2014-01-01 00:00:01 CST" "2014-01-01 00:00:02 CST"
> attr(x, "index")
[1] 1388556000 1388556001 1388556002
attr(,"tzone")
[1] ""
attr(,"tclass")
[1] "POSIXct" "POSIXt"
At one point in the past, the internal index was stored as POSIXct
, but we changed the underlying structure for performance reasons. You can see that the public API didn't change, however.