Hello,
The object 'a' exists if function f() not in the global environment
where g() is defined. R is in fact going up, but to the global
environment and not finding 'a'. Try, as an example, the following.
f <- function(x) {
g <- function(y) { y + a }
a <- 5
g(x)
}
f(2) # 7
Now R is finding 'a'. Because g() exists in the environment of f() (like
'a' does.)
Hope this helps,
Rui Barradas
Em 24-06-2013 21:27, David Kulp escreveu:
According to
http://cran.r-project.org/doc/contrib/Fox-Companion/appendix-scope.pdf and
other examples online, I am to believe that R resolves variables using lexical
scoping by following the frames up the call stack. However, that's not working
for me. For example, the following code, taken from the reference above fails
instead of returning 7. What am I doing wrong? Thanks!
f <- function(x) { a<-5; g(x) }
g <- function(y) { y + a }
f(2)
Error in g(x) : object 'a' not found
[[alternative HTML version deleted]]
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.