[R] about eval and eval.parent

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Nov 18 16:58:00 CET 2005


On Fri, 18 Nov 2005, ronggui wrote:

> x<-1
> f<-function(){
> x<-3
> eval(substitute(x+y,list(y=10)))
> }
> f() #13
>
> x<-1
> f<-function(){
> x<-3
> eval(substitute(x+y,list(y=10)), envir = sys.frame(sys.parent()))
> }
> f() #11
>
> x<-1
> f<-function(){
> x<-3
> eval.parent(substitute(x+y,list(y=10)))
> }
> f()#11
>
>
> the help page says:
>    "If 'envir' is
>     not specified, then 'sys.frame(sys.parent())', the environment
>     where the call to 'eval' was made is used. "
>
>     But it seems that the first one is not equal to the second one.

I think you have plucked this out of context.  It is the default value for 
envir which is parent.frame() (and was sys.frame(sys.parent()), the same 
thing).  Default and explicit arguments are not evaluated in the same 
frames, and so giving sys.frame(sys.parent()) as an explicit argument is 
different from taking the default.

Once you remember the last sentence, there is no mystery.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list