Skip to content

What the difference between .Golbalenv and package:base?

12 messages · PO SU, John McKown, MacQueen, Don +2 more

#
Dear rusers,

? ? As we know, there are a lot of environments in the search() path, such as?? .Golbalenv and package:base .
And ?i can just use ?.Golbalenv$a ,.Golbalenv$b to use the virable, ?but i must use as.envrionment("package:base") to find virable, i feel it not very convenient.


For example, when i use the following codes to add a new env into the search() path.
[1] 2
[1] 2
?I must always convert the name to the environment, How can i just use the following form:
[1] 2
[1] 2







--

PO SU
mail: desolator88 at 163.com 
Majored in Statistics from SJTU
#
On Mon, Aug 25, 2014 at 1:07 AM, PO SU <rhelpmaillist at 163.com> wrote:
You might want to try:

new_name <- new.env();
# or if you prefer (such as in a function)
assign("new_name",new.env(),envir=.GlobalEnv);
#
# You may now assign variable into this similar to:
new_name$a <- 2;
gvar <- new_name$a; # get the variable a from environment new_name
gvar <- get("a",envir=new_name); #same thing, but wordy
attach(new_name);
a
gvar <- a;
#
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.





--

PO SU
mail: desolator88 at 163.com
Majored in Statistics from SJTU
At 2014-08-25 09:53:37, "John McKown" <john.archie.mckown at gmail.com> wrote:

  
  
#
On Mon, Aug 25, 2014 at 11:19 AM, PO SU <rhelpmaillist at 163.com> wrote:
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:
[1] ".GlobalEnv"        "new_name"          "new_name"
"tools:rstudio"
 [5] "package:graphics"  "package:grDevices" "package:utils"
"package:datasets"
 [9] "package:methods"   "Autoloads"         "package:base"
[1] 2
[1] "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.
[1] ".GlobalEnv"        "new_name"          "tools:rstudio"
"package:stats"
 [5] "package:graphics"  "package:grDevices" "package:utils"
"package:datasets"
 [9] "package:methods"   "Autoloads"         "package:base"
[1] "new_name"
NULL
[1] 2
[1] "x"
[1] 2
If you wanted to use string values in the first two commands above,
then perhaps:
[1] ".GlobalEnv"        "new_name"          "tools:rstudio"
"package:graphics"
 [5] "package:grDevices" "package:utils"     "package:datasets"
"package:methods"
 [9] "Autoloads"         "package:base"
character(0)
[1] "a"
[1] 2
[1] "a"
Likewise you can do:
[1] ".GlobalEnv"        "tools:rstudio"     "package:stats"
"package:graphics"
 [5] "package:grDevices" "package:utils"     "package:datasets"
"package:methods"
 [9] "Autoloads"         "package:base"
[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"
function (x, ...)
UseMethod("time")
<bytecode: 0x000000000a4e8b00>
<environment: namespace:stats>
function (x, ...)
UseMethod("time")
<bytecode: 0x000000000a4e8b00>
<environment: namespace:stats>

  
    
#
On Mon, Aug 25, 2014 at 12:26 PM, John McKown
<john.archie.mckown at gmail.com> wrote:
<snip>
The above does not work because I did it incorrectly. The code below
is the proper way to do this.
[1] 2
[1] 2
[1] "a"
[1] "a" "b"
[1] "b"
It appears that what happens in the original is that the attach() does
not point to the environment, but creates its own copy. In the second
case, attach() creates the environment, then the new line assigns a
"pointer" to that same physical environment to the variable new_name.
I'm learning some _interesting_ things from this discussion.
#
Tks for all your details, after your introduction, i really read the ?attach carefully, and i now understand the argument "pos" now, but in my opnion, the details in the function "attach" may do the as.environment(pos) for me.?
And i also understand that, "attach" will copy the attached envir,and add the copied envir into the search path list as you showed ?the examples to me.
After all, i want to ask a last question:
I notice that,
[1] "R_GlobalEnv"
<environment: R_GlobalEnv>
Error in as.environment("R_GlobalEnv") :?
? no item called "R_GlobalEnv" on the search list
<environment: R_GlobalEnv>
[1] ""
<environment: package:stats>
attr(,"name")
[1] "package:stats"
attr(,"path")
[1] "C:/Program Files/R/R-3.1.1/library/stats"


I am really confused now, while?as.environment("package:stats") can be work by convert the name of the environment stats, the?environmentName returns "" !
And get the .GlobalEnv from ".GlobalEnv" ,but can't form?"R_GlobalEnv" which is actually the name of the environment.
















--

PO SU
mail: desolator88 at 163.com 
Majored in Statistics from SJTU
At 2014-08-26 01:26:28, "John McKown" <john.archie.mckown at gmail.com> wrote:
#
On Mon, Aug 25, 2014 at 1:08 PM, PO SU <rhelpmaillist at 163.com> wrote:
You are now much deeper into the internals of R than my knowledge.
Perhaps one of the truly wise ones here knows. Or this may be a better
question for the people on r-devel. It is really getting more towards
the "why" of R rather than the "how to".
#
Put simply,
   .GlobalEnv    stores objects you create
   package:base  contains functions and objects provided by R itself

You don?t need to use   .GlobalEnv$a   to use the variable named a. Just
is ?a? by itself.

 a <- 4
 b <- 2*a
print(a)
print(b)

Not necessary to use
  print(.GlobalEnv$a)

Similarly, to find an object in the base package, just type its name.

I don?t know what you are trying to do, or why you think you have to use
.GlobalEnv$a   
But in more than 20 years of using R for many different tasks, I have
never had to do that.

Furthermore, if you are new to R (which I would guess is the case), it
seems unlikely to me that you need to work with environments or use
attach() or assign(). In the vast majority of cases there are simpler ways
that are easier to understand.

You are aware, I hope, that
  > ls('.GlobalEnv')
  > ls(1)
  > ls()
all return the same result?
#
First, sorry for my pool english expression which make you misunderstanding of my original purpose.

Sometimes, suppose ?a object in both stats and base, then i type the object name, then after R search the search() list, R will use the object in stats, is it right?( I just suppose, stats can be any package which libraried into R.)
Then i know that, .GlobalEnv or globalenv() is the global environment object, baseenv() returns the base environment object.
I also know that, i can convert the environment name into the real environment object by using stats<-as.environment("package:stats"), ?And the stats environment's name can be obtained using environmentName(stats), but it returns "". ? (why?)
Then i use ?environmentName(.GlobalEnv) then i get "R_GlobalEnv", then i use?as.environment("R_GlobalEnv"), it does't work.(why?)


In one word, as.environment(x), x is somthing not the environment's name.?


But, when i add a environment into the search() list, after i attr(newenvir,"name")<-"new_name"
I can use the??as.environment("new_name") to obtain the added environment. (why?)


Hope you understand my meaning :)












--

PO SU
mail: desolator88 at 163.com 
Majored in Statistics from SJTU
At 2014-08-26 02:51:54, "MacQueen, Don" <macqueen1 at llnl.gov> wrote:
#
I would refer to base::somename or stat::somename if necessary, and I never use attach, get or assign.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.
On August 25, 2014 9:07:58 PM PDT, PO SU <rhelpmaillist at 163.com> wrote:
#
as.environment(characterString) maps an entry from the output of
search() to the environment at the named position in the search list.
as.environment(number) maps an index into the output of search() to
the the environment at that position in the search list.  If
'characterString' is not in the output of search() or 'number' is not
in seq_along(search()) then as.environment throws an error.  As far as
I can tell, as.environment does not deal with the name of the
environment at all.  (When you attach an environment, attach will add
a name attribute to the copied environment so the attached
environment's name matches the name on the output of search(), but I
don't think as.environment ever looks at that attribute.)

Bill Dunlap
TIBCO Software
wdunlap tibco.com
On Mon, Aug 25, 2014 at 9:07 PM, PO SU <rhelpmaillist at 163.com> wrote:
#
So, the decisive factor is  whether the input string be on the search() name list, and not related with the envir's name attribute.
When we using attach, it is becasue the name attribute just match the search() name list(or say,search() name list just use the name attribute), so as.environment() can work  well. 
Tks!


--

PO SU
mail: desolator88 at 163.com
Majored in Statistics from SJTU
At 2014-08-27 00:02:07, "William Dunlap" <wdunlap at tibco.com> wrote: