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 "h
oo <- function() { height <- 3; a() }
> foo() # height does not exist
>
> -Original Message-
> From: Gabor Grothendieck [mailto:ggrothendi...@gmail.com]
> Sent: Sunday, April 18, 2010 8:00 PM
> To: e...@cs.uic.edu
> Cc: r-sig-geo@stat.math.ethz.ch
> Subject: Re:
;-function()simple()
foo <- function() { height <- 3; a() }
foo() # height does not exist
-Original Message-
From: Gabor Grothendieck [mailto:ggrothendi...@gmail.com]
Sent: Sunday, April 18, 2010 8:00 PM
To: e...@cs.uic.edu
Cc: r-sig-geo@stat.math.ethz.ch
Subject: Re: [R-sig-Geo] R Scopi
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 envir
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 ther