j.c.rougier@durham.ac.uk
2002-Oct-11 18:38 UTC
Odd outcome of attr with environments (PR#2148)
Hi everyone, I think the following error is slightly odd:> fred <- new.env() > happy <- function() fred > happy()<environment: 0x8a425b8>> attr(happy(), "foo") <- 1:10Error: invalid (NULL) left side of assignment But naturally this works> attr(fred, "foo") <- 1:10 > fred<environment: 0x8a425b8> attr(,"foo") [1] 1 2 3 4 5 6 7 8 9 10 I would have thought that "fred" and "happy()" were synonymous, so I was surprised that I could not attach an attribute to the environment using "happy()". Of course in practice I can get round this by doing> sad <- happy() > attr(sad, "foo") <- 5:1 > sad<environment: 0x8a425b8> attr(,"foo") [1] 5 4 3 2 1> happy()<environment: 0x8a425b8> attr(,"foo") [1] 5 4 3 2 1 but I wondered what was going on. Cheers, Jonathan. --please do not edit the information below-- Version: platform = i686-pc-linux-gnu arch = i686 os = linux-gnu system = i686, linux-gnu status = major = 1 minor = 6.0 year = 2002 month = 10 day = 01 language = R Search Path: .GlobalEnv, Autoloads, package:base -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Fri, 11 Oct 2002 j.c.rougier@durham.ac.uk wrote:> Hi everyone, > > I think the following error is slightly odd: > > > fred <- new.env() > > happy <- function() fred > > happy() > <environment: 0x8a425b8> > > attr(happy(), "foo") <- 1:10 > Error: invalid (NULL) left side of assignment >It's nothing to do with environments. happy() is not an variable, so you can't modify it: eg R> f<-function(){ 42} R> f()<-1 Error: invalid (NULL) left side of assignment R> attr(f(),"foo")<-1 Error: invalid (NULL) left side of assignment R> f()[1]<-1 Error: invalid (NULL) left side of assignment -thomas -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._