Re: [R-sig-Geo] R Scoping Rules

2010-04-19 Thread Thomas Lumley
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

Re: [R-sig-Geo] R Scoping Rules

2010-04-18 Thread Gabor Grothendieck
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:

Re: [R-sig-Geo] R Scoping Rules

2010-04-18 Thread Stephen G. Eick
;-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

Re: [R-sig-Geo] R Scoping Rules

2010-04-18 Thread Gabor Grothendieck
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

[R-sig-Geo] R Scoping Rules

2010-04-18 Thread Stephen G. Eick
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