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]]
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 <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.-- Sent from my phone. Please excuse my brevity.
?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 <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.
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
On Tue, Apr 4, 2023 at 7:26?PM 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.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 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]]
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 *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::yError: '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 <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]]