The following *might* be of use to you. If you can predict what the various function invocations will do, I think you have a reasonable grasp of how lexical scoping works in R (contrary or supplementary opinions welcome). It is the sort of thing you will find in the references also. If this is all obvious, sorry for wasting your time. ####################### search() ls() dat <- list(x =2) attach(dat,2) search() f <- function(){ g <- function() x x <- 3 g} h <- f() g <- function()x ls() h() g() detach(dat) h() g() ########################## ## Here is what this gives starting with an empty .GlobalEnv. ##################################> search()[1] ".GlobalEnv" "package:tools" "package:lattice" "tools:rstudio" [5] "package:stats" "package:graphics" "package:grDevices" "package:utils" [9] "package:datasets" "package:methods" "Autoloads" "package:base"> ls()character(0)> dat <- list(x =2) > attach(dat,2) > search()[1] ".GlobalEnv" "dat" "package:tools" "package:lattice" [5] "tools:rstudio" "package:stats" "package:graphics" "package:grDevices" [9] "package:utils" "package:datasets" "package:methods" "Autoloads" [13] "package:base"> f <- function(){+ g <- function() x + x <- 3 + g}> h <- f() > g <- function()x > ls()[1] "dat" "f" "g" "h"> h()[1] 3> g()[1] 2> detach(dat) > h()[1] 3> g()Error in g() : object 'x' not found -- Bert On Tue, Apr 4, 2023 at 6:56?AM akshay kulkarni <akshay_e4 at hotmail.com> wrote:> Dear Members, > I have the following code typed at the > console prompt: > > y <- x*10 > > X has not been defined and the above code throws an object not found > error. That is, the global environment does not contain x. Why doesn't it > look further in the environment stack, like that of packages? There are > thousands of packages that contain the variable named x. Of course, that > happens if the above code is in a function (or does it?). > > What concept of R is at work in this dichotomy? > > THanking you, > Yours sincerely, > AKSHAY M KULKARNI > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >[[alternative HTML version deleted]]
obviously, everyone has different opinions on what's useful but I always found this document quite helpful. I think, in the past, someone said that there are some incorrect statements in but I'm not sure what they are. https://askming.github.io/study_notes/Stats_Comp/Note-How%20R%20searches%20and%20finds%20stuff.html On Tue, Apr 4, 2023 at 7:06?PM Bert Gunter <bgunter.4567 at gmail.com> wrote:> The following *might* be of use to you. If you can predict what the various > function invocations will do, I think you have a reasonable grasp of how > lexical scoping works in R (contrary or supplementary opinions welcome). > It is the sort of thing you will find in the references also. If this is > all obvious, sorry for wasting your time. > ####################### > search() > ls() > dat <- list(x =2) > attach(dat,2) > search() > f <- function(){ > g <- function() x > x <- 3 > g} > h <- f() > g <- function()x > ls() > h() > g() > detach(dat) > h() > g() > > ########################## > ## Here is what this gives starting with an empty .GlobalEnv. > ################################## > > > search() > [1] ".GlobalEnv" "package:tools" "package:lattice" > "tools:rstudio" > [5] "package:stats" "package:graphics" "package:grDevices" > "package:utils" > [9] "package:datasets" "package:methods" "Autoloads" > "package:base" > > ls() > character(0) > > dat <- list(x =2) > > attach(dat,2) > > search() > [1] ".GlobalEnv" "dat" "package:tools" > "package:lattice" > [5] "tools:rstudio" "package:stats" "package:graphics" > "package:grDevices" > [9] "package:utils" "package:datasets" "package:methods" > "Autoloads" > [13] "package:base" > > f <- function(){ > + g <- function() x > + x <- 3 > + g} > > h <- f() > > g <- function()x > > ls() > [1] "dat" "f" "g" "h" > > h() > [1] 3 > > g() > [1] 2 > > detach(dat) > > h() > [1] 3 > > g() > Error in g() : object 'x' not found > > -- Bert > > > On Tue, Apr 4, 2023 at 6:56?AM akshay kulkarni <akshay_e4 at hotmail.com> > wrote: > > > Dear Members, > > I have the following code typed at the > > console prompt: > > > > y <- x*10 > > > > X has not been defined and the above code throws an object not found > > error. That is, the global environment does not contain x. Why doesn't it > > look further in the environment stack, like that of packages? There are > > thousands of packages that contain the variable named x. Of course, that > > happens if the above code is in a function (or does it?). > > > > What concept of R is at work in this dichotomy? > > > > THanking you, > > Yours sincerely, > > AKSHAY M KULKARNI > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > 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. > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >[[alternative HTML version deleted]]
dear Bert, I could predict most of them, but,of course, you have not wasted my time! Thanking you, Yours sincerely, AKSHAY M KULKARNI ________________________________ From: Bert Gunter <bgunter.4567 at gmail.com> Sent: Wednesday, April 5, 2023 4:36 AM To: akshay kulkarni <akshay_e4 at hotmail.com> Cc: R help Mailing list <r-help at r-project.org> Subject: Re: [R] on lexical scoping.... The following *might* be of use to you. If you can predict what the various function invocations will do, I think you have a reasonable grasp of how lexical scoping works in R (contrary or supplementary opinions welcome). It is the sort of thing you will find in the references also. If this is all obvious, sorry for wasting your time. ####################### search() ls() dat <- list(x =2) attach(dat,2) search() f <- function(){ g <- function() x x <- 3 g} h <- f() g <- function()x ls() h() g() detach(dat) h() g() ########################## ## Here is what this gives starting with an empty .GlobalEnv. ##################################> search()[1] ".GlobalEnv" "package:tools" "package:lattice" "tools:rstudio" [5] "package:stats" "package:graphics" "package:grDevices" "package:utils" [9] "package:datasets" "package:methods" "Autoloads" "package:base"> ls()character(0)> dat <- list(x =2) > attach(dat,2) > search()[1] ".GlobalEnv" "dat" "package:tools" "package:lattice" [5] "tools:rstudio" "package:stats" "package:graphics" "package:grDevices" [9] "package:utils" "package:datasets" "package:methods" "Autoloads" [13] "package:base"> f <- function(){+ g <- function() x + x <- 3 + g}> h <- f() > g <- function()x > ls()[1] "dat" "f" "g" "h"> h()[1] 3> g()[1] 2> detach(dat) > h()[1] 3> g()Error in g() : object 'x' not found -- Bert On Tue, Apr 4, 2023 at 6:56?AM akshay kulkarni <akshay_e4 at hotmail.com<mailto:akshay_e4 at hotmail.com>> wrote: Dear Members, I have the following code typed at the console prompt: y <- x*10 X has not been defined and the above code throws an object not found error. That is, the global environment does not contain x. Why doesn't it look further in the environment stack, like that of packages? There are thousands of packages that contain the variable named x. Of course, that happens if the above code is in a function (or does it?). What concept of R is at work in this dichotomy? THanking you, Yours sincerely, AKSHAY M KULKARNI [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org<mailto:R-help at r-project.org> mailing list -- To UNSUBSCRIBE and more, see 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. [[alternative HTML version deleted]]