What the difference between .Golbalenv and package:base?
On Mon, Aug 25, 2014 at 11:19 AM, PO SU <rhelpmaillist at 163.com> wrote:
As you know, in the search path, there is .GlobalEnv, package:stats and so on, why do we need to convert the character "package:stats" to the stats environment. I mean, why don't let package:stats be a environment type object like .GlobalEnv,but let it be a string ? Hope you understand my meaning for my pool english expression way.
Yes, you have Sorry for my misunderstanding of what were originally saying. I _think_ that I now understand. The fault is likely my concentrating on the wrong part of your original email. To test my ability to understand, I submit the following possibility:
new_name<-new.env(); attach(new_name) search()
[1] ".GlobalEnv" "new_name" "new_name" "tools:rstudio" [5] "package:graphics" "package:grDevices" "package:utils" "package:datasets" [9] "package:methods" "Autoloads" "package:base"
assign("a",2,pos="new_name")
a
[1] 2
ls()
[1] "new_name"
ls(pos="new_name")
[1] "a"
Note the use of pos= instead of envir=. That seems to be the key here.
I hope this was of more use to you. One problem that I have noticed is
that you can not get to the value of "a" by using "new_name$a", but
must use the get() function like: get('a',pos='new_name');
Please be very aware of the following, very confusing fact:
Referencing a variable can not have the expected results.
new_name <- new.env() attach(new_name) search()
[1] ".GlobalEnv" "new_name" "tools:rstudio" "package:stats" [5] "package:graphics" "package:grDevices" "package:utils" "package:datasets" [9] "package:methods" "Autoloads" "package:base"
assign("a",2,"new_name")
ls()
[1] "new_name"
new_name$a
NULL
get("a",pos="new_name")
[1] 2
new_name$a <- 'x' new_name$a;
[1] "x"
get("a",pos="new_name")
[1] 2
If you wanted to use string values in the first two commands above, then perhaps:
attach(NULL,name="new_name") search()
[1] ".GlobalEnv" "new_name" "tools:rstudio" "package:graphics" [5] "package:grDevices" "package:utils" "package:datasets" "package:methods" [9] "Autoloads" "package:base"
assign("a",2,pos="new_name")
ls()
character(0)
ls(pos="new_name")
[1] "a"
a
[1] 2
# or even
ls("new_name")
[1] "a"
Likewise you can do:
search()
[1] ".GlobalEnv" "tools:rstudio" "package:stats" "package:graphics" [5] "package:grDevices" "package:utils" "package:datasets" "package:methods" [9] "Autoloads" "package:base"
ls(pos="package:stats")
[1] "acf" "acf2AR" "add.scope" [4] "add1" "addmargins" "aggregate" [7] "aggregate.data.frame" "aggregate.ts" "AIC" [10] "alias" "anova" "ansari.test" ... [436] "variable.names" "varimax" "vcov" [439] "weighted.mean" "weighted.residuals" "weights" [442] "wilcox.test" "window" "window<-" [445] "write.ftable" "xtabs"
get("time",pos="package:stats")
function (x, ...)
UseMethod("time")
<bytecode: 0x000000000a4e8b00>
<environment: namespace:stats>
x<-get("time",pos="package:stats")
x
function (x, ...)
UseMethod("time")
<bytecode: 0x000000000a4e8b00>
<environment: namespace:stats>
# note that the get() basically created a variable in the global environment whose value was # the same as in the package. But you can change the value of "x" in the global environment # and it won't affect the value in the package. And vice versa, if you could update "x" in # the package, but that can't be done because packages seem to be locked and read-only.
-- PO SU mail: desolator88 at 163.com Majored in Statistics from SJTU
There is nothing more pleasant than traveling and meeting new people! Genghis Khan Maranatha! <>< John McKown