Skip to content
Back to formatted view

Raw Message

Message-ID: <1372107143.28622.YahooMailNeo@web142605.mail.bf1.yahoo.com>
Date: 2013-06-24T20:52:23Z
From: arun
Subject: Lexical scoping is not what I expect
In-Reply-To: <F50F8583-AC2A-4E48-BC36-E193663C593A@fiksu.com>

?f1<- function(x){env<- parent.frame();env$a<-5; g(x)}
f1(2)
#[1] 7
?f1(7)
#[1] 12
?f1(5)
#[1] 10
A.K.



________________________________
From: David Kulp <dkulp at fiksu.com>
To: "r-help at r-project.org" <r-help at r-project.org> 
Sent: Monday, June 24, 2013 4:27 PM
Subject: [R] Lexical scoping is not what I expect


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 at 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.