Re: [R] on lexical scoping....
Dear richard, Bulls eye! thanks for your pointed reply...! THanking you, Yours sincerely, AKSHAY M KULKARNI From: Richard O'Keefe Sent: Wednesday, April 5, 2023 10:30 AM To: akshay kulkarni Cc: R help Mailing list Subject: Re: [R] on lexical scoping R *does* search the environment stack. > search() [1] ".GlobalEnv""package:stats" "package:graphics" [4] "package:grDevices" "package:utils" "package:datasets" [7] "package:methods" "Autoloads" "package:base What you seem to be missing is that a package may contain bindings that it does not export, as the wording of this error message reminds us: > utils::y Error: 'y' is not an exported object from 'namespace:utils' So when package/namespace goes onto the environment stack, it's only the *exported* bindings that become visible. On Wed, 5 Apr 2023 at 01:56, akshay kulkarni mailto: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<mailto: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.
Re: [R] on lexical scoping....
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 Sent: Wednesday, April 5, 2023 4:36 AM To: akshay kulkarni Cc: R help Mailing list 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 mailto: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<mailto: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.
Re: [R] on lexical scoping....
Hi Duncan. I read it a long time ago so it's probably best if I read it again ALONG with your corrections. Every detail matters with these concepts so thanks a lot. Mark On Wed, Apr 5, 2023 at 5:12 AM Duncan Murdoch wrote: > It seems mostly correct. Here are a few quibbles: > > - I don't think "owner" is a good description of the parent environment. > Usually when I use owner in computing, there's an implication that the > owner controls what it owns, is responsible for allocating and > destroying it, etc. Parent environments are targets of a pointer from > other environments that list them as their "enclosure", but they have no > record of their children. > > - The analogy that all roads lead to the search list is wrong. There's > no reason you couldn't create an environment whose parent was the empty > environment. But with the qualifier "when we start in R_GlobalEnv" it's > correct, so this complaint is just about the wording. > > - The description of what Imports does is wrong. What is described > there is what the `import()` directive in NAMESPACE does. The Imports > line in DESCRIPTION just guarantees that the named package is loaded, it > doesn't import anything. And the `importFrom()` directive in NAMESPACE > isn't discussed. > > - It gets bad near the end in the "Curveball" section. I'd just skip > that. It's not at all true that all functions in a package have the > package's namespace environment as their environment. There are lots of > real examples where that's not true. Functions that maintain persistent > records (e.g. the base function conflictRules()) often have a private > environment, and occasionally packages import functions from other > packages by simple assignment, so they end up in the namespace > environment of the importer but still have the namespace environment of > the exporter associated with them. And the last diagram (the revised > one with all solid lines) is just misleading. > > Duncan > > On 04/04/2023 7:53 p.m., Mark Leeds 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 > 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 > >> 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
Re: [R] on lexical scoping....
It seems mostly correct. Here are a few quibbles: - I don't think "owner" is a good description of the parent environment. Usually when I use owner in computing, there's an implication that the owner controls what it owns, is responsible for allocating and destroying it, etc. Parent environments are targets of a pointer from other environments that list them as their "enclosure", but they have no record of their children. - The analogy that all roads lead to the search list is wrong. There's no reason you couldn't create an environment whose parent was the empty environment. But with the qualifier "when we start in R_GlobalEnv" it's correct, so this complaint is just about the wording. - The description of what Imports does is wrong. What is described there is what the `import()` directive in NAMESPACE does. The Imports line in DESCRIPTION just guarantees that the named package is loaded, it doesn't import anything. And the `importFrom()` directive in NAMESPACE isn't discussed. - It gets bad near the end in the "Curveball" section. I'd just skip that. It's not at all true that all functions in a package have the package's namespace environment as their environment. There are lots of real examples where that's not true. Functions that maintain persistent records (e.g. the base function conflictRules()) often have a private environment, and occasionally packages import functions from other packages by simple assignment, so they end up in the namespace environment of the importer but still have the namespace environment of the exporter associated with them. And the last diagram (the revised one with all solid lines) is just misleading. Duncan On 04/04/2023 7:53 p.m., Mark Leeds 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 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 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]] __
Re: [R] on lexical scoping....
R *does* search the environment stack. > search() [1] ".GlobalEnv""package:stats" "package:graphics" [4] "package:grDevices" "package:utils" "package:datasets" [7] "package:methods" "Autoloads" "package:base What you seem to be missing is that a package may contain bindings that it does not export, as the wording of this error message reminds us: > utils::y Error: 'y' is not an exported object from 'namespace:utils' So when package/namespace goes onto the environment stack, it's only the *exported* bindings that become visible. On Wed, 5 Apr 2023 at 01:56, akshay kulkarni 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.
Re: [R] on lexical scoping....
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 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 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 >> 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.
Re: [R] on lexical scoping....
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 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 > 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.
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 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.
Re: [R] on lexical scoping....
Dear Duncan, THanks a lot..!! THanking you, Yours sincerely, AKSHAY M KULKARNI From: Duncan Murdoch Sent: Tuesday, April 4, 2023 8:49 PM To: akshay kulkarni ; Deepayan Sarkar Cc: R help Mailing list Subject: Re: [R] on lexical scoping No, there are lots of situations where that doesn't make sense. You don't want to have to define local copies of the functions from every package you use, for example. I think the takeaway is to learn how R scoping works, and keep things simple. That's one reason I tend to avoid "tidyverse" packages. There are a lot of really good ideas in those packages, but "tidy evaluation" is far too complicated and hard to understand, and I think it confuses people, so they don't understand the really very simple R scoping rules. Duncan Murdoch On 04/04/2023 10:59 a.m., akshay kulkarni wrote: > Dear Duncan, > THanks for the reply...! > > So the takeaway is that define the symbol in the same environment before > using it right!? > > Thanking you, > Yours sincerely, > AKSHAY M KULKARNI > > *From:* Duncan Murdoch > *Sent:* Tuesday, April 4, 2023 8:21 PM > *To:* akshay kulkarni ; Deepayan Sarkar > > *Cc:* R help Mailing list > *Subject:* Re: [R] on lexical scoping > You can't change the basic way R searches, but you can ask for a > different kind of search. For example, to see if "x" exists, you can use > >exists("x") > > and it will do the default search, but > >exists("x", inherits = FALSE) > > will only look in the current environment. The get() function has a > similar argument which returns the value > > Unfortunately these functions have overly complicated argument lists > because they are based on functions in S from 30-40 years ago, and it > had very different scoping rules. My advice would be to ignore the > "where" and "frame" arguments, and always use "envir" if you want to say > where to look. > > Duncan Murdoch > > On 04/04/2023 10:28 a.m., akshay kulkarni wrote: >> Dear Deepayan, >>THanks for the pithy, pointed reply. >> >> But isn't it risky? Can I somehow get a warning when x is not defined in the >> global environment but takes on a value from one of the loaded packages? any >> packages for that? >> >> THanking you, >> Yours sincerely, >> AKSHAY M KULKARNI >> >> From: Deepayan Sarkar >> Sent: Tuesday, April 4, 2023 7:51 PM >> To: akshay kulkarni >> Cc: R help Mailing list >> Subject: Re: [R] on lexical scoping >> >> >> >> On Tue, Apr 4, 2023 at 7:26 PM akshay kulkarni >> mailto: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. >> >> That is not the correct interpretation of the error. R will happily evaluate >> >> y <- pi*10 >> >> even if the global environment does not contain pi. The "environments" where >> R will look is given by >> >> search() >> >> If you manage to find a package that defines 'x' (and exports it), attaching >> it will put the package on the search path, and then your call will indeed >> no longer give an error. >> >> -Deepayan >> >> 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<mailto:R-help@r-project.org> mailing list -- To >> UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help > <https://stat.ethz.ch/mailman/listinfo/r-help> >> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > <http://www.R-project.org/posting-guide.html> >> and provide commented, minimal, self-contained, reproducible code. >> >>[[alternative HTML version deleted]] >> >
Re: [R] on lexical scoping....
No, there are lots of situations where that doesn't make sense. You don't want to have to define local copies of the functions from every package you use, for example. I think the takeaway is to learn how R scoping works, and keep things simple. That's one reason I tend to avoid "tidyverse" packages. There are a lot of really good ideas in those packages, but "tidy evaluation" is far too complicated and hard to understand, and I think it confuses people, so they don't understand the really very simple R scoping rules. Duncan Murdoch On 04/04/2023 10:59 a.m., akshay kulkarni wrote: Dear Duncan, THanks for the reply...! So the takeaway is that define the symbol in the same environment before using it right!? Thanking you, Yours sincerely, AKSHAY M KULKARNI *From:* Duncan Murdoch *Sent:* Tuesday, April 4, 2023 8:21 PM *To:* akshay kulkarni ; Deepayan Sarkar *Cc:* R help Mailing list *Subject:* Re: [R] on lexical scoping You can't change the basic way R searches, but you can ask for a different kind of search. For example, to see if "x" exists, you can use exists("x") and it will do the default search, but exists("x", inherits = FALSE) will only look in the current environment. The get() function has a similar argument which returns the value Unfortunately these functions have overly complicated argument lists because they are based on functions in S from 30-40 years ago, and it had very different scoping rules. My advice would be to ignore the "where" and "frame" arguments, and always use "envir" if you want to say where to look. Duncan Murdoch On 04/04/2023 10:28 a.m., akshay kulkarni wrote: Dear Deepayan, THanks for the pithy, pointed reply. But isn't it risky? Can I somehow get a warning when x is not defined in the global environment but takes on a value from one of the loaded packages? any packages for that? THanking you, Yours sincerely, AKSHAY M KULKARNI From: Deepayan Sarkar Sent: Tuesday, April 4, 2023 7:51 PM To: akshay kulkarni Cc: R help Mailing list Subject: Re: [R] on lexical scoping On Tue, Apr 4, 2023 at 7:26 PM akshay kulkarni mailto: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. That is not the correct interpretation of the error. R will happily evaluate y <- pi*10 even if the global environment does not contain pi. The "environments" where R will look is given by search() If you manage to find a package that defines 'x' (and exports it), attaching it will put the package on the search path, and then your call will indeed no longer give an error. -Deepayan 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<mailto:R-help@r-project.org> mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help <https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html <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 <https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html <http://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code. __ 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.
Re: [R] on lexical scoping....
Dear Duncan, THanks for the reply...! So the takeaway is that define the symbol in the same environment before using it right!? Thanking you, Yours sincerely, AKSHAY M KULKARNI From: Duncan Murdoch Sent: Tuesday, April 4, 2023 8:21 PM To: akshay kulkarni ; Deepayan Sarkar Cc: R help Mailing list Subject: Re: [R] on lexical scoping You can't change the basic way R searches, but you can ask for a different kind of search. For example, to see if "x" exists, you can use exists("x") and it will do the default search, but exists("x", inherits = FALSE) will only look in the current environment. The get() function has a similar argument which returns the value Unfortunately these functions have overly complicated argument lists because they are based on functions in S from 30-40 years ago, and it had very different scoping rules. My advice would be to ignore the "where" and "frame" arguments, and always use "envir" if you want to say where to look. Duncan Murdoch On 04/04/2023 10:28 a.m., akshay kulkarni wrote: > Dear Deepayan, >THanks for the pithy, pointed reply. > > But isn't it risky? Can I somehow get a warning when x is not defined in the > global environment but takes on a value from one of the loaded packages? any > packages for that? > > THanking you, > Yours sincerely, > AKSHAY M KULKARNI > > From: Deepayan Sarkar > Sent: Tuesday, April 4, 2023 7:51 PM > To: akshay kulkarni > Cc: R help Mailing list > Subject: Re: [R] on lexical scoping > > > > On Tue, Apr 4, 2023 at 7:26 PM akshay kulkarni > mailto: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. > > That is not the correct interpretation of the error. R will happily evaluate > > y <- pi*10 > > even if the global environment does not contain pi. The "environments" where > R will look is given by > > search() > > If you manage to find a package that defines 'x' (and exports it), attaching > it will put the package on the search path, and then your call will indeed no > longer give an error. > > -Deepayan > > 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<mailto: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.
Re: [R] on lexical scoping....
You can't change the basic way R searches, but you can ask for a different kind of search. For example, to see if "x" exists, you can use exists("x") and it will do the default search, but exists("x", inherits = FALSE) will only look in the current environment. The get() function has a similar argument which returns the value Unfortunately these functions have overly complicated argument lists because they are based on functions in S from 30-40 years ago, and it had very different scoping rules. My advice would be to ignore the "where" and "frame" arguments, and always use "envir" if you want to say where to look. Duncan Murdoch On 04/04/2023 10:28 a.m., akshay kulkarni wrote: Dear Deepayan, THanks for the pithy, pointed reply. But isn't it risky? Can I somehow get a warning when x is not defined in the global environment but takes on a value from one of the loaded packages? any packages for that? THanking you, Yours sincerely, AKSHAY M KULKARNI From: Deepayan Sarkar Sent: Tuesday, April 4, 2023 7:51 PM To: akshay kulkarni Cc: R help Mailing list Subject: Re: [R] on lexical scoping On Tue, Apr 4, 2023 at 7:26 PM akshay kulkarni mailto: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. That is not the correct interpretation of the error. R will happily evaluate y <- pi*10 even if the global environment does not contain pi. The "environments" where R will look is given by search() If you manage to find a package that defines 'x' (and exports it), attaching it will put the package on the search path, and then your call will indeed no longer give an error. -Deepayan 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<mailto: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. __ 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.
Re: [R] on lexical scoping....
On 04/04/2023 10:35 a.m., akshay kulkarni wrote: Dear Duncan, THanks for the reply. I am looking at the technical point. The behavior you just described, as far as I know, is only for functions right? No, not at all. Every function you write in R has an associated environment (which you can find using environment(fn)). That only applies to searches taking place while evaluating the function. The starting point is a temporary environment to hold local variables, called the "evaluation frame". The parent of that environment is the environment given by environment(fn). When you type something at the top level, evaluation is done very similarly, except that the starting point for the search is the global environment. THre is no documentation ever, which says that the code looks for x in the search path. Could you please point me to some resources where I can find further information on lexical scoping for the code "typed" in the console (but not in a function)? See section 3.5.1 in the R Language Definition for a description of the relation between the global environment and the search list. See section 2.1.10 in that manual for how R looks up variables. Duncan Murdoch THanking you, Yours sincerely, AKSHAY M KULKARNI *From:* Duncan Murdoch *Sent:* Tuesday, April 4, 2023 7:48 PM *To:* akshay kulkarni ; R help Mailing list *Subject:* Re: [R] on lexical scoping On 04/04/2023 9:56 a.m., akshay kulkarni 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? First, some background: Packages are associated with multiple environments. There is the internal one that the package sees, and the external one that contains just the exports. These are sometimes called the "package" environment and the "namespace" environment, but I don't think those names are used consistently. (There's another one containing the imports, but for these purposes, it's indistinguishable from the internal one.) When a package is loaded by loadNamespace("pkg"), nothing happens in the global environment: no new variables are visible. When it is attached by library("pkg"), a lot more happens. First, it is loaded as above, then the search list is modified. The global environment is entry 1 in the search list; it stays there, but its "parent" is set to a copy of the external environment from the new package, and the parent of that environment is set to the previous parent, the second entry in the search list. Okay, so now you search for "x" in the global environment, and it's not there. It then goes to the other entries in the search list, which are typically external environments from various packages. None of those packages export "x", so it is not found. It doesn't matter if those packages use "x" without exporting it, because R won't look at internal environments in this kind of search. And it doesn't matter what happens in other packages that are not on the search list (i.e. not "attached" because you never called library() or require() on them), because they just aren't in the chain of environments where R looks. Duncan Murdoch __ 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.
Re: [R] on lexical scoping....
Dear Ducan, Very informative! THanks a lot! THanking you, Yours sincerely, AKSHAY M KULKARNI From: Duncan Murdoch Sent: Tuesday, April 4, 2023 8:14 PM To: akshay kulkarni ; R help Mailing list Subject: Re: [R] on lexical scoping On 04/04/2023 10:35 a.m., akshay kulkarni wrote: > Dear Duncan, > THanks for the reply. > > I am looking at the technical point. The behavior you just described, as > far as I know, is only for functions right? No, not at all. Every function you write in R has an associated environment (which you can find using environment(fn)). That only applies to searches taking place while evaluating the function. The starting point is a temporary environment to hold local variables, called the "evaluation frame". The parent of that environment is the environment given by environment(fn). When you type something at the top level, evaluation is done very similarly, except that the starting point for the search is the global environment. THre is no documentation > ever, which says that the code looks for x in the search path. Could you > please point me to some resources where I can find further information > on lexical scoping for the code "typed" in the console (but not in a > function)? See section 3.5.1 in the R Language Definition for a description of the relation between the global environment and the search list. See section 2.1.10 in that manual for how R looks up variables. Duncan Murdoch > > THanking you, > Yours sincerely, > AKSHAY M KULKARNI > > > *From:* Duncan Murdoch > *Sent:* Tuesday, April 4, 2023 7:48 PM > *To:* akshay kulkarni ; R help Mailing list > > *Subject:* Re: [R] on lexical scoping > On 04/04/2023 9:56 a.m., akshay kulkarni 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? >> > > First, some background: > > Packages are associated with multiple environments. There is the > internal one that the package sees, and the external one that contains > just the exports. > > These are sometimes called the "package" environment and the "namespace" > environment, but I don't think those names are used consistently. > (There's another one containing the imports, but for these purposes, > it's indistinguishable from the internal one.) > > When a package is loaded by loadNamespace("pkg"), nothing happens in the > global environment: no new variables are visible. > > When it is attached by library("pkg"), a lot more happens. First, it is > loaded as above, then the search list is modified. The global > environment is entry 1 in the search list; it stays there, but its > "parent" is set to a copy of the external environment from the new > package, and the parent of that environment is set to the previous > parent, the second entry in the search list. > > Okay, so now you search for "x" in the global environment, and it's not > there. It then goes to the other entries in the search list, which are > typically external environments from various packages. None of those > packages export "x", so it is not found. > > It doesn't matter if those packages use "x" without exporting it, > because R won't look at internal environments in this kind of search. > > And it doesn't matter what happens in other packages that are not on the > search list (i.e. not "attached" because you never called library() or > require() on them), because they just aren't in the chain of > environments where R looks. > > Duncan Murdoch [[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.
Re: [R] on lexical scoping....
Dear Jeff, THanks a lot for the pithy reply... Thanking you, Yours sincerely, AKSHAY M KULKARNI From: Jeff Newmiller Sent: Tuesday, April 4, 2023 7:43 PM To: r-help@r-project.org ; akshay kulkarni ; R help Mailing list Subject: Re: [R] on lexical scoping Namespaces. Packages only export specific object names from their namespaces. But few instances of x would be found there. Also, function argument lists are not added to the search path until the functions are running, and then the search path only goes through the environments in which the running functions were defined, not through the call stack. Read Advanced R. [1] [1] https://adv-r.hadley.nz/environments.html On April 4, 2023 6:56:04 AM PDT, akshay kulkarni 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. -- Sent from my phone. Please excuse my brevity. [[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.
Re: [R] on lexical scoping....
Dear Bert, THanks a lot. I will take a look at those... THanking you, Yours sincerely, AKSHAY M KULKARNI From: Bert Gunter Sent: Tuesday, April 4, 2023 7:48 PM To: akshay kulkarni Cc: R help Mailing list Subject: Re: [R] on lexical scoping ?search and ?environment See also "The R Language Definition" manual for similar such questions. -- Bert On Tue, Apr 4, 2023 at 6:56 AM akshay kulkarni 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.
Re: [R] on lexical scoping....
Dear Duncan, THanks for the reply. I am looking at the technical point. The behavior you just described, as far as I know, is only for functions right? THre is no documentation ever, which says that the code looks for x in the search path. Could you please point me to some resources where I can find further information on lexical scoping for the code "typed" in the console (but not in a function)? THanking you, Yours sincerely, AKSHAY M KULKARNI From: Duncan Murdoch Sent: Tuesday, April 4, 2023 7:48 PM To: akshay kulkarni ; R help Mailing list Subject: Re: [R] on lexical scoping On 04/04/2023 9:56 a.m., akshay kulkarni 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? > First, some background: Packages are associated with multiple environments. There is the internal one that the package sees, and the external one that contains just the exports. These are sometimes called the "package" environment and the "namespace" environment, but I don't think those names are used consistently. (There's another one containing the imports, but for these purposes, it's indistinguishable from the internal one.) When a package is loaded by loadNamespace("pkg"), nothing happens in the global environment: no new variables are visible. When it is attached by library("pkg"), a lot more happens. First, it is loaded as above, then the search list is modified. The global environment is entry 1 in the search list; it stays there, but its "parent" is set to a copy of the external environment from the new package, and the parent of that environment is set to the previous parent, the second entry in the search list. Okay, so now you search for "x" in the global environment, and it's not there. It then goes to the other entries in the search list, which are typically external environments from various packages. None of those packages export "x", so it is not found. It doesn't matter if those packages use "x" without exporting it, because R won't look at internal environments in this kind of search. And it doesn't matter what happens in other packages that are not on the search list (i.e. not "attached" because you never called library() or require() on them), because they just aren't in the chain of environments where R looks. Duncan Murdoch [[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.
Re: [R] on lexical scoping....
Dear Deepayan, THanks for the pithy, pointed reply. But isn't it risky? Can I somehow get a warning when x is not defined in the global environment but takes on a value from one of the loaded packages? any packages for that? THanking you, Yours sincerely, AKSHAY M KULKARNI From: Deepayan Sarkar Sent: Tuesday, April 4, 2023 7:51 PM To: akshay kulkarni Cc: R help Mailing list Subject: Re: [R] on lexical scoping On Tue, Apr 4, 2023 at 7:26 PM akshay kulkarni mailto: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. That is not the correct interpretation of the error. R will happily evaluate y <- pi*10 even if the global environment does not contain pi. The "environments" where R will look is given by search() If you manage to find a package that defines 'x' (and exports it), attaching it will put the package on the search path, and then your call will indeed no longer give an error. -Deepayan 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<mailto: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.
Re: [R] on lexical scoping....
On Tue, Apr 4, 2023 at 7:26 PM akshay kulkarni 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. That is not the correct interpretation of the error. R will happily evaluate y <- pi*10 even if the global environment does not contain pi. The "environments" where R will look is given by search() If you manage to find a package that defines 'x' (and exports it), attaching it will put the package on the search path, and then your call will indeed no longer give an error. -Deepayan 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.
Re: [R] on lexical scoping....
On 04/04/2023 9:56 a.m., akshay kulkarni 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? First, some background: Packages are associated with multiple environments. There is the internal one that the package sees, and the external one that contains just the exports. These are sometimes called the "package" environment and the "namespace" environment, but I don't think those names are used consistently. (There's another one containing the imports, but for these purposes, it's indistinguishable from the internal one.) When a package is loaded by loadNamespace("pkg"), nothing happens in the global environment: no new variables are visible. When it is attached by library("pkg"), a lot more happens. First, it is loaded as above, then the search list is modified. The global environment is entry 1 in the search list; it stays there, but its "parent" is set to a copy of the external environment from the new package, and the parent of that environment is set to the previous parent, the second entry in the search list. Okay, so now you search for "x" in the global environment, and it's not there. It then goes to the other entries in the search list, which are typically external environments from various packages. None of those packages export "x", so it is not found. It doesn't matter if those packages use "x" without exporting it, because R won't look at internal environments in this kind of search. And it doesn't matter what happens in other packages that are not on the search list (i.e. not "attached" because you never called library() or require() on them), because they just aren't in the chain of environments where R looks. Duncan Murdoch __ 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.
Re: [R] on lexical scoping....
?search and ?environment See also "The R Language Definition" manual for similar such questions. -- Bert On Tue, Apr 4, 2023 at 6:56 AM akshay kulkarni 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. __ 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.
Re: [R] on lexical scoping....
Namespaces. Packages only export specific object names from their namespaces. But few instances of x would be found there. Also, function argument lists are not added to the search path until the functions are running, and then the search path only goes through the environments in which the running functions were defined, not through the call stack. Read Advanced R. [1] [1] https://adv-r.hadley.nz/environments.html On April 4, 2023 6:56:04 AM PDT, akshay kulkarni 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. -- 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.