Skip to content
Prev 30967 / 398506 Next

lexical scope

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()
..but...


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.


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