[R] lexical scope

Robin Hankin r.hankin at auckland.ac.nz
Tue Apr 22 06:07:39 CEST 2003

Hi everyone

another documented feature that was a bit unexpected for me:

R> x <- 19
R> f <- function(t){t+x}
R> f(100)
[1] 119

--as expected: x is visible from within f()

R> g <- function(a){x <- 1e99 ; return(f(a))}
R> g(4)
[1] 23

--the "x" that is visible from within g() is just 19, which is not the
  one I expected it to find.

R> rm(x)
R> g(4)
Error in f(a) : Object "x" not found

--g() looks in the first search path place and finds it empty,
  returning an error.

Why doesn't g()  "keep looking" ?
How do I tell g() where to find x?
Where to look for documentation for this?



Robin Hankin, Lecturer,
School of Geography and Environmental Science
Tamaki Campus
Private Bag 92019 Auckland
New Zealand

r.hankin at auckland.ac.nz
tel 0064-9-373-7599 x6820; FAX 0064-9-373-7042

More information about the R-help mailing list