Skip to content
Prev 69713 / 398503 Next

scoping issues (was help with eval)

You intended quote() rather than expression(), I believe.  If I do
this works.

R has lexical scoping, so with
'a' is looked for in the frame of the function (not defined there) and 
then in the environment of the function.
<environment: R_GlobalEnv>

since show.a was defined in the workspace.  The envir arg of eval() is 
used to find the object(s), here `show.a', not to sets its environment.
So my first version works because 'a' is part of the expression.

Perhaps you intended something like

init.env <- function() {
  a <- 200
  show.a <- function() {a}
  environment()
}
my.env <- init.env()
eval(quote(show.a()),envir=my.env)

which works, and is a common R idiom (although via local() or new.env()).

However, you could just do
[1] 200
[1] 200

BTW, use typeof() to see the difference here.
On Fri, 13 May 2005, Whit Armstrong wrote: