thmsfuller066 at gmail.com
2010-May-13 17:16 UTC
[Rd] What functions are called internally in R to resolve what variable is referred?
Hello All, If I refer to a variable 'x', 'x' will be searched in the current frame or the parent frame, or the parent of the parent frame, etc., until it is found (or not found at all)? Could you please show me what code in R source that handles this? Is it in the C code or the R code? Thanks, Tom
Simon Urbanek
2010-May-13 18:22 UTC
[Rd] What functions are called internally in R to resolve what variable is referred?
On May 13, 2010, at 1:16 PM, thmsfuller066 at gmail.com wrote:> Hello All, > > If I refer to a variable 'x', 'x' will be searched in the current > frame or the parent frame, or the parent of the parent frame, etc., > until it is found (or not found at all)? Could you please show me what > code in R source that handles this? Is it in the C code or the R code? >At R level it is done automatically during evaluation (but you can so something similar using get() function in R). Internally (in C), findVar(symbol, env) in src/main/envir.c does the job by using findVarInFrame3() recursively for parent environments. There is slight exception if you use x() since that does a search with the restriction that x must be a function and thus uses findFun() instead. Cheers, Simon
William Dunlap
2010-May-13 18:55 UTC
[Rd] What functions are called internally in R to resolve whatvariable is referred?
> From: r-devel-bounces at r-project.org > [mailto:r-devel-bounces at r-project.org] On Behalf Of > thmsfuller066 at gmail.com > Sent: Thursday, May 13, 2010 10:16 AM > To: r-devel at r-project.org > Subject: [Rd] What functions are called internally in R to > resolve whatvariable is referred? > > Hello All, > > If I refer to a variable 'x', 'x' will be searched in the current > frame or the parent frame, or the parent of the parent frame, etc., > until it is found (or not found at all)?Be careful with your terms here. While a function is evaluating its 'parent frame' is the environment of the function that called it and its 'parent environment' is the environment in which it was defined. R searches though the chain of parent environments, not parent frames. The following example shows the difference between the parent environment and the parent frame. # setup x <- "Global x" f1 <- function() { c(x=x, xFromParentFrame=get("x", envir=parent.frame()), xFromParentEnv=get("x", envir=parent.env(environment()))) } f0 <- function(i) { x<-paste("f0's x: i=", i, sep="") # Note how subf0 and f1 have identical definitions: # they only differ in where they were created. subf0 <- function () { c(x = x, xFromParentFrame = get("x", envir = parent.frame()), xFromParentEnv = get("x", envir = parent.env(environment()))) } list(subf0=subf0(), f1=f1(), FUN=subf0) } > # run the functions > z <- f0(17) > z $subf0 x xFromParentFrame xFromParentEnv "f0's x: i=17" "f0's x: i=17" "f0's x: i=17" $f1 x xFromParentFrame xFromParentEnv "Global x" "f0's x: i=17" "Global x" $FUN function () { c(x = x, xFromParentFrame = get("x", envir = parent.frame()), xFromParentEnv = get("x", envir = parent.env(environment()))) } <environment: 0x292af70> > z$FUN() x xFromParentFrame xFromParentEnv "f0's x: i=17" "Global x" "f0's x: i=17" Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> Could you please show me what > code in R source that handles this? Is it in the C code or the R code? > > Thanks, > Tom