[R] Understanding eval

Gabor Grothendieck ggrothendieck at gmail.com
Sun Dec 23 02:51:57 CET 2007


Duncan has already pointed out that consideration of promises is
what is missing in the description but in addition the way lm and
other functions in R get around it is to use match.call like this:

bar2 <- function(er) {
	mc <- match.call()
	mc[[1]] <- as.name("foo")
	names(mc)[[2]] <- "expr"
	eval.parent(mc)
}
bar2(a)

On Dec 22, 2007 3:30 PM, Charilaos Skiadas <cskiadas at gmail.com> wrote:
> After many hours of debugging code, I came to the conclusion that I
> have a fundamental misunderstanding regarding eval, and hope that
> someone here can explain to me, why the following code acts as it does:
>
> foo <- function(expr) {
>   eval(substitute(expr), envir=list(a=5), enclos=parent.frame())
> }
> bar <- function(er) {
>   foo(er)
> }
>
>  > foo(a)
> [1] 5
>  > bar(a)
> Error in eval(expr, envir, enclos) : object "a" not found
>
>
> Now, regarding the "bar(a)" call, this is my understanding of what
> happens, hoping someone will correct me where I'm wrong.
>
> 1) bar is called. Its evaluation frame contains the association "er=a".
> 2) bar calls foo. So foo is called, and its evaluation frame contains
> the association "expr=er", with enclosing environment the local
> environment of bar.
> 3) foo calls eval.
> 4) eval starts by evaluating "substitute(expr)" in foo's environment.
> "substitute" then locates expr in foo's environment, and replaces it
> with er. So the result of this process is the symbol er, which is
> what will now be evaluated by eval.
> 5) eval then creates the environment where this evaluation will take
> place. It does that by creating an environment containing the frame
> "a=5", and with enclosing environment the parent frame of foo, which
> is bar's environment.
> 6) So, as I understand it, the symbol "er" is going to now be
> evaluated in an environment where a is set to 5 and er is set to a,
> along with whatever is in the user's workspace.
> 7) So the first step now is looking up a definition for er. Nothing
> is found in the current frame, so the evaluation proceeds to bar's
> environment, where the association "er=a" is found, so er is replaced
> by a.
> 8) Now, and perhaps this is where I misunderstand things, the lookup
> for a will take place. My thinking was that the lookup would start
> from the evaluation environment that eval created, and hence would
> locate the a=5 value. But this is clearly not what happens.
>
> Anyway, hope someone will correct me where I'm wrong, and explain to
> me what I am doing wrong, and ideally how to diagnose such things.
>
> Thanks,
> Haris Skiadas
> Department of Mathematics and Computer Science
> Hanover College
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list