[R] What the difference between .Golbalenv and package:base?
PO SU
rhelpmaillist at 163.com
Wed Aug 27 05:44:47 CEST 2014
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:
>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:
>> 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:
>>>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?
>>>
>>>
>>>--
>>>Don MacQueen
>>>
>>>Lawrence Livermore National Laboratory
>>>7000 East Ave., L-627
>>>Livermore, CA 94550
>>>925-423-1062
>>>
>>>
>>>
>>>
>>>
>>>On 8/24/14, 11:07 PM, "PO SU" <rhelpmaillist at 163.com> wrote:
>>>
>>>>
>>>>
>>>>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.
>>>>
>>>>
>>>>
>>>>> tmp<-attach(NULL,name="new_name")
>>>>> assign("a",2,envir=as.environment("new_name"))
>>>>> a
>>>>[1] 2
>>>>> as.environment("new_name")$a
>>>>[1] 2
>>>> I must always convert the name to the environment, How can i just use
>>>>the following form:
>>>>
>>>>
>>>>
>>>>> tmp<-attach(NULL,name="new_name")
>>>>> assign("a",2,envir=new_name) #like using .GlobalEnv
>>>>> a
>>>>[1] 2
>>>>> new_name$a
>>>>
>>>>[1] 2
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>--
>>>>
>>>>PO SU
>>>>mail: desolator88 at 163.com
>>>>Majored in Statistics from SJTU
>>>>______________________________________________
>>>>R-help at r-project.org mailing list
>>>>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 at r-project.org mailing list
>> 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]]
More information about the R-help
mailing list