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. Thank you! 2005-11-18 ------ Deparment of Sociology Fudan University My new mail addres is ronggui.huang at gmail.com Blog:http://sociology.yculblog.com
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.No, it isn't. This is because default arguments are evaluated inside the function but explicit argumnets are evaluated in the calling frame In the first one, sys.frame(sys.parent()) is evaluated in the environment inside f(), and so refers to the frame from which f() was called. That's what you would expect from pass-by-value In the second one, sys.frame(sys.parent()) is evaluated inside substitute() and refers to the environment inside f(). That might be surprising, which is why it is documented. -thomas
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