[R] eval in parent frame: scoping rule confusion

Thomas Lumley tlumley at u.washington.edu
Fri Mar 15 04:07:16 CET 2002

On Thu, 14 Mar 2002, Jonathan Q. Li wrote:

> Hi all,
> Look at the following "stupid" function that is created just to show the
> problem:
> outlayer <- function(x)
> {
> 	inlayer <- function(z){
> 		e1 <- parent.frame()
> 		eval(expression(y<-log(z)),e1)
>  	}
> 	inlayer(10)
> y
> }
> I have a compilation error when running this. My purpose is to create "y"
> variable equal to log(z) in
> the frame of "outlay". How can I achieve this?

The problem is that you can't evaluate y<-log(z) in the outer frame since
there is no z in the outer frame.  You want to evaluate y<-log(10) (or
whatever z happens to be)
One way to do this is to replace your eval() with

Whether that's the best way depends on what your real example looks like.


r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list