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]]
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 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. >
On Mon, Jun 24, 2013 at 4:27 PM, David Kulp <dkulp at fiksu.com> wrote:> 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 >What you have described is called dynamic scoping. What lexical scoping is is that it resolves free variables based on where the function is _defined_, not where it is run and it has nothing to do with the call stack. a is a free variable in g and so when g is run R looks up a in the environment where g was defined which is the global environment and there is no a there. -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.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.
On Jun 24, 2013, at 3:27 PM, David Kulp <dkulp at fiksu.com> wrote:> 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 foundYou need to follow the full example code in John's Appendix: f <- function (x) x + a # Here 'a' is being defined in the global environment a <- 10 # As is 'x' here x <- 5 # Note that 10 + 5 would be 15, # 12 is returned showing that x = 2 and not x = 5 # is being use within f()> f(2)[1] 12 # Here is where your code starts # missing the preceding code where 'a' was # defined globally f <- function(x) { a<-5; g(x) } g <- function(y) y + a # Now it works, showing that 'a <- 5' within f() is not part of the # value returned by g(), which is the goal of John's example code :-)> f(2)[1] 12 Regards, Marc Schwartz
On 13-06-24 4:27 PM, David Kulp wrote:> 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.You appear to have misread it. Lexical scoping follows the chain of environments where functions were defined. It ignores the call stack. Duncan Murdoch 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. >
I second Ellison sentiments of "almost never". One main reason is readability on later viewing. Yes, as Duncan says global variables can sometimes be handy and make functions quick to write, but using a formal argument in the call is always clearer. Terry Therneau On 06/27/2013 05:00 AM, r-help-request at r-project.org wrote:> On 13-06-26 6:57 AM, S Ellison wrote: >> > >> > >>> >> -----Original Message----- >>> >> It may be helpful not to worry about the technical details, >>> >> just to look at the source code defining the function: if it >>> >> is defined in a place where a variable can be seen, it can >>> >> see that variable. >> > >> > I too find R's lexical scoping rules straightforward. >> > However, I'd say that if your code relies on lexical scoping to find something, you should probably rewrite your code. >> > >> > The number of times I've seen new R users get unexpected results because they haven't noticed that their function is referencing a parent environment instead of a locally defined variable or argument is past counting. >> > >> > Of course there are times when it's useful and sensible to have globally defined variables that can be accessed within a function. But they are very rare; as a default, I'd recommend avoiding it if at all possible. If your function needs something from outside, pass it as an argument. >> > > I would say the meaning of "probably" in your 2nd sentence depends quite > a bit on the user. For beginners, it's "almost certainly". For people > who are comfortable with the concept, it's just "maybe". > > I would agree that in most cases the only global objects that functions > should reference are other functions, but small nested functions are > quite safe, and it's sometimes useful to create functions in a local > environment so they have persistent memory. > > Duncan Murdoch >