Skip to content
Prev 59294 / 398502 Next

Lexical Scoping: eval(expr,envir=)

On Thu, 18 Nov 2004 12:05:34 +0100, Eric Lecoutre
<lecoutre at stat.ucl.ac.be> wrote :
To work with lexical scoping, you could do it as

 myObject  <- function(){
    a <- 1 
     list(
       a=function() a,
       "a<-" = function(x, value) a <<- value,
       foo=function(b)
       {
       cat("b:",b,"\n")
       cat("a:", a,"\n")
       }
     )
   }

Then you can access the a property pretty easily, but changing it is
really ugly:
[1] 1
Error in cat("b:", b, "\n") : Argument "b" is missing, with no default
b: 1 
a: 1
b: 1 
a: 4 

It would be slightly nicer (but still ugly) if this worked:
Error: invalid function in complex assignment

but it doesn't.

Duncan Murdoch