[R] evaluation revisited

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Wed Jan 28 12:26:53 CET 2009


Gabor Grothendieck wrote:
> The argument to eval.parent is evaluated before eval.parent
> ever sees it. 

really?  eval.parent is just a regular r function, a wrapper for eval
with envir=parent.frame().  the arguments to eval.parent are passed to
eval *unevaluated* (as promises), and are only evaluated when eval needs
them.  here's a modified eval.parent:

my.eval.parent = function(expr, n=1) {
    print('foo')
    p = parent.frame(n+1)
    eval(expr, p) }

my.eval.parent({print(1); 2})
# prints 'foo' before printing 1 and returning 2



>  Try issuing this command before you run your
> code:
>
> debug(eval.parent)
>
> and look at the value of the arguments as passed to eval.parent
> in the debugger.
>   

well, when you are in the debugger and look at the value of the
arguments you actually force the promises, so no wonder you see them
evaluated.  if you don't look at them, they're not evaluated:

trace(eval)
trace(parent.frame)
eval.parent({print(1);2})
# calling parent.frame
# calling eval
# printing 1 (after parent.frame and eval have been called)
# returning 2

vQ




More information about the R-help mailing list