Leading off with you can only have two things in an environment definitely indicates this should be read with a skeptical eye.
Although the title of "Advanced R" may be more scary than someone writing notes on GitHub like a bro, IMHO Adv R is quite readable for anyone interested in questions like this with fewer wrong assertions to unlearn later. On April 4, 2023 4:53:38 PM PDT, Mark Leeds <marklee...@gmail.com> wrote: >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.4...@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...@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@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@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@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. -- Sent from my phone. Please excuse my brevity. ______________________________________________ R-help@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.