Saturday, 15 March 2014

r - Assigning attributes to an environment can lead to surprises? -



r - Assigning attributes to an environment can lead to surprises? -

according r language definition:

unlike other r objects, environments not copied when passed functions or used in assignments. thus, if assign same environment several symbols , alter one, others alter too. in particular, assigning attributes environment can lead surprises.

> e = new.env() > attr(e, "mynumber", true) null > attr(e, "mynumber") = 555 > attr(e, "mynum") [1] 555

what surprises exactly?

its examples such assign 2 e's at attribute , f's at attribute automatically changes too:

e <- new.env() attr(e, "at") <- 1 f <- e attr(e, "at") <- 2 attributes(f) ## $at ## [1] 2

r

No comments:

Post a Comment