Can someone help me with this simple example? sq <- function() { y <- x^2 y } myfunc <- function() { x <- 10 sq() } myfunc() executing the above in R yields:> myfunc()Error in sq() : Object "x" not found I understand that R's scoping rules cause it to look for "x" in the environment in which "sq" was defined (the global environment in this case). But in this case "x" is defined inside the calling function, not the environment in which "sq" was defined. Is there a way to tell R to look in the calling function for "x" ? I have tried the following variants of "eval" such as eval(sq(),parent.frame()) with no success. Thanks for your help. Regards, Whit> R.Version()$platform [1] "i386-pc-mingw32" $arch [1] "i386" $os [1] "mingw32" $system [1] "i386, mingw32" $status [1] "" $major [1] "1" $minor [1] "9.1" $year [1] "2004" $month [1] "06" $day [1] "21" $language [1] "R">
Whit Armstrong <Whit.Armstrong <at> tudor.com> writes: : : Can someone help me with this simple example? : : sq <- function() { : y <- x^2 : y : } : : myfunc <- function() { : x <- 10 : sq() : } : : myfunc() : : executing the above in R yields: : > myfunc() : Error in sq() : Object "x" not found : : I understand that R's scoping rules cause it to look for "x" in the : environment in which "sq" was defined (the global environment in this case). : But in this case "x" is defined inside the calling function, not the : environment in which "sq" was defined. : : Is there a way to tell R to look in the calling function for "x" ? : : I have tried the following variants of "eval" such as : eval(sq(),parent.frame()) with no success. : Here are two approaches: 1. have myfunc change sq's environment to the current environment: sq <- function() { y <- x^2; y } myfunc <- function() { x <- 10; environment(sq) <- environment(); sq() } myfunc() 2. modify sq itself to get x from the parent frame. We use get in the example below but you could alternately replace get(...) with eval.parent(substitute(x)) if you prefer to use eval: sq <- function() { y <- get("x", parent.frame())^2; y } myfunc <- function() { x <- 10; sq() } myfunc()
Anyone saved the upcoming R course announcement for Princeton and Boston? Please email me... Thanks Eugene.
On Thu, 9 Sep 2004 12:01:56 -0400 , Whit Armstrong <Whit.Armstrong at tudor.com> wrote :>Can someone help me with this simple example? > >sq <- function() { > y <- x^2 > y >} > >myfunc <- function() { > x <- 10 > sq() >} > >myfunc() > > >executing the above in R yields: >> myfunc() >Error in sq() : Object "x" not found > >I understand that R's scoping rules cause it to look for "x" in the >environment in which "sq" was defined (the global environment in this case). >But in this case "x" is defined inside the calling function, not the >environment in which "sq" was defined. > >Is there a way to tell R to look in the calling function for "x" ?The easiest (and best) is to pass x as an argument to sq. Duncan Murdoch
On Thu, 9 Sep 2004, Whit Armstrong wrote:> Can someone help me with this simple example? > > sq <- function() { > y <- x^2 > y > } > > myfunc <- function() { > x <- 10 > sq() > } > > myfunc() > > > executing the above in R yields: >> myfunc() > Error in sq() : Object "x" not found > > I understand that R's scoping rules cause it to look for "x" in the > environment in which "sq" was defined (the global environment in this case). > But in this case "x" is defined inside the calling function, not the > environment in which "sq" was defined. > > Is there a way to tell R to look in the calling function for "x" ?yes, but you almost certainllyy don't want to. In a real example you would want to either 1/ Pass x as an argument (most likely) 2/ Define sq() inside myfunc() 3/ define myfunc() inside sq() 4/ Pass the name of x as an argument (eg quote(x) or ~x) While you can fake dynamic scope in R, there is no point in doing it with a variable whose name is hard-coded. The only reason to allow access to a non-local variable x with a fixed name is to preserve its value across calls to the function, but with dynamic scope the "x" may be a completely different variable each time. -thomas