An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20100418/c8ce6a69/attachment.pl>
R Scoping Rules
5 messages · Stephen G. Eick, Gabor Grothendieck, Thomas Lumley
R uses lexical scope. That means that when a variable is not found in
a function it next looks where the function was _defined_ and not
where the function was called from. Since simple is defined in the
global environment it looks there for height if can`t find it within
simple. The parent environment is where the function was defined and
the parent frame is where it was called from. To force it to look in
the parent frame rather than the parent environment try this:
simple <- function(env = parent.frame()) {
if(exists("height", env)) cat("height exists\n")
else cat("height does not exist\n")
}
foo <- function() { height <- 3; simple() }
foo() # height exists
On Sun, Apr 18, 2010 at 8:27 PM, Stephen G. Eick <eick at vistracks.com> wrote:
simple<-function() {
if(exists("height"))
cat("height exists\n")
else
cat("height does not exist\n")
}
foo<-function() {
height<-10
simple()
}
foo()
height does not exist Can somebody please explain why "simple" does not find the "height" variable when I run it? Is there an easy way to make R use scoping rules like python where it searches over all frames to find the value of an symbol? Thanks, Steve Eick ? ? ? ?[[alternative HTML version deleted]]
_______________________________________________ R-sig-Geo mailing list R-sig-Geo at stat.math.ethz.ch https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Thanks. This makes sense. Is there any way to make R search over every
frame all the way back to the global frame?
simple <- function(env = parent.frame()) {
if(exists("height", env)) cat("height exists\n")
else cat("height does not exist\n")
}
a<-function()simple()
foo <- function() { height <- 3; a() }
foo() # height does not exist
-----Original Message-----
From: Gabor Grothendieck [mailto:ggrothendieck at gmail.com]
Sent: Sunday, April 18, 2010 8:00 PM
To: eick at cs.uic.edu
Cc: r-sig-geo at stat.math.ethz.ch
Subject: Re: [R-sig-Geo] R Scoping Rules
R uses lexical scope. That means that when a variable is not found in
a function it next looks where the function was _defined_ and not
where the function was called from. Since simple is defined in the
global environment it looks there for height if can`t find it within
simple. The parent environment is where the function was defined and
the parent frame is where it was called from. To force it to look in
the parent frame rather than the parent environment try this:
simple <- function(env = parent.frame()) {
if(exists("height", env)) cat("height exists\n")
else cat("height does not exist\n")
}
foo <- function() { height <- 3; simple() }
foo() # height exists
On Sun, Apr 18, 2010 at 8:27 PM, Stephen G. Eick <eick at vistracks.com> wrote:
simple<-function() {
if(exists("height"))
cat("height exists\n")
else
cat("height does not exist\n")
}
foo<-function() {
height<-10
simple()
}
foo()
height does not exist Can somebody please explain why "simple" does not find the "height"
variable
when I run it? Is there an easy way to make R use scoping rules like python where it searches over all frames to find the value of an symbol? Thanks, Steve Eick ? ? ? ?[[alternative HTML version deleted]]
_______________________________________________ R-sig-Geo mailing list R-sig-Geo at stat.math.ethz.ch https://stat.ethz.ch/mailman/listinfo/r-sig-geo
You can use parent.frame(n), see ?parent.frame and ?get, but its probably not a good idea since you will wind up with a function that does not work in the way the rest of R works.
On Sun, Apr 18, 2010 at 9:17 PM, Stephen G. Eick <eick at vistracks.com> wrote:
Thanks. ?This makes sense. ?Is there any way to make R search over every
frame all the way back to the global frame?
simple <- function(env = parent.frame()) {
? if(exists("height", env)) cat("height exists\n")
? else cat("height does not exist\n")
}
a<-function()simple()
foo <- function() { height <- 3; a() }
foo() # height does not exist
-----Original Message-----
From: Gabor Grothendieck [mailto:ggrothendieck at gmail.com]
Sent: Sunday, April 18, 2010 8:00 PM
To: eick at cs.uic.edu
Cc: r-sig-geo at stat.math.ethz.ch
Subject: Re: [R-sig-Geo] R Scoping Rules
R uses lexical scope. That means that when a variable is not found in
a function it next looks where the function was _defined_ and not
where the function was called from. ? Since simple is defined in the
global environment it looks there for height if can`t find it within
simple. ?The parent environment is where the function was defined and
the parent frame is where it was called from. ?To force it to look in
the parent frame rather than the parent environment try this:
simple <- function(env = parent.frame()) {
? if(exists("height", env)) cat("height exists\n")
? else cat("height does not exist\n")
}
foo <- function() { height <- 3; simple() }
foo() # height exists
On Sun, Apr 18, 2010 at 8:27 PM, Stephen G. Eick <eick at vistracks.com> wrote:
simple<-function() {
if(exists("height"))
cat("height exists\n")
else
cat("height does not exist\n")
}
foo<-function() {
height<-10
simple()
}
foo()
height does not exist Can somebody please explain why "simple" does not find the "height"
variable
when I run it? Is there an easy way to make R use scoping rules like python where it searches over all frames to find the value of an symbol? Thanks, Steve Eick ? ? ? ?[[alternative HTML version deleted]]
_______________________________________________ R-sig-Geo mailing list R-sig-Geo at stat.math.ethz.ch https://stat.ethz.ch/mailman/listinfo/r-sig-geo
On Sun, 18 Apr 2010, Stephen G. Eick wrote:
simple<-function() {
if(exists("height"))
cat("height exists\n")
else
cat("height does not exist\n")
}
foo<-function() {
height<-10
simple()
}
foo()
height does not exist Can somebody please explain why "simple" does not find the "height" variable when I run it?
'height' is neither a local variable, nor visible in the environment where 'simple()' was defined. That's the point of static lexical scope -- the scope depends on what was visible where the function was defined. You seem to want dynamic scope, where variables in the calling environment are visible. This can be faked with eval.parent(), but isn't how R naturally works.
Is there an easy way to make R use scoping rules like python where it searches over all frames to find the value of an symbol?
R *does* use scoping rules like python (since python 2.1). Your example doesn't work in python, for exactly the same reason.
def simple():
... print height ...
def foo():
... height = 10 ... simple() ...
foo()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in foo
File "<stdin>", line 2, in simple
NameError: global name 'height' is not defined
-thomas
Thomas Lumley Assoc. Professor, Biostatistics
tlumley at u.washington.edu University of Washington, Seattle