What the difference between .Golbalenv and package:base?

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

What the difference between .Golbalenv and package:base?

PO SU


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: [hidden email]
Majored in Statistics from SJTU
______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: What the difference between .Golbalenv and package:base?

John McKown
On Mon, Aug 25, 2014 at 1:07 AM, PO SU <[hidden email]> 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: [hidden email]
> Majored in Statistics from SJTU

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;


--
There is nothing more pleasant than traveling and meeting new people!
Genghis Khan

Maranatha! <><
John McKown

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: What the difference between .Golbalenv and package:base?

PO SU
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: [hidden email]
Majored in Statistics from SJTU



At 2014-08-25 09:53:37, "John McKown" <[hidden email]> wrote:

>On Mon, Aug 25, 2014 at 1:07 AM, PO SU <[hidden email]> 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: [hidden email]
>> Majored in Statistics from SJTU
>
>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;
>
>
>--
>There is nothing more pleasant than traveling and meeting new people!
>Genghis Khan
>
>Maranatha! <><
>John McKown

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: What the difference between .Golbalenv and package:base?

John McKown
On Mon, Aug 25, 2014 at 11:19 AM, PO SU <[hidden email]> 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: [hidden email]
> Majored in Statistics from SJTU


--
There is nothing more pleasant than traveling and meeting new people!
Genghis Khan

Maranatha! <><
John McKown

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: What the difference between .Golbalenv and package:base?

John McKown
On Mon, Aug 25, 2014 at 12:26 PM, John McKown
<[hidden email]> wrote:
<snip>

> 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
>>

The above does not work because I did it incorrectly. The code below
is the proper way to do this.

> attach(NULL,name="new_name")
> new_name<-as.environment("new_name")
> assign("a",2,pos="new_name")
> get("a",pos="new_name")
[1] 2
> new_name$a
[1] 2
> ls(pos="new_name")
[1] "a"
> new_name$b<-'b'
> ls(pos="new_name")
[1] "a" "b"
> get('b',pos='new_name')
[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.

--
There is nothing more pleasant than traveling and meeting new people!
Genghis Khan

Maranatha! <><
John McKown

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: What the difference between .Golbalenv and package:base?

PO SU
In reply to this post by John McKown

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,

> environmentName(.GlobalEnv)
[1] "R_GlobalEnv"
> as.environment(".GlobalEnv")
<environment: R_GlobalEnv>

>as.environment("R_GlobalEnv")
Error in as.environment("R_GlobalEnv") : 
  no item called "R_GlobalEnv" on the search list
> .GlobalEnv
<environment: R_GlobalEnv>

> environmentName("package:stats")
[1] ""
> as.environment("package:stats")
<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: [hidden email]
Majored in Statistics from SJTU



At 2014-08-26 01:26:28, "John McKown" <[hidden email]> wrote:

>On Mon, Aug 25, 2014 at 11:19 AM, PO SU <[hidden email]> 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: [hidden email]
>> Majored in Statistics from SJTU
>
>
>--
>There is nothing more pleasant than traveling and meeting new people!
>Genghis Khan
>
>Maranatha! <><
>John McKown
______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: What the difference between .Golbalenv and package:base?

John McKown
On Mon, Aug 25, 2014 at 1:08 PM, PO SU <[hidden email]> wrote:

>
> 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,
>
>> environmentName(.GlobalEnv)
> [1] "R_GlobalEnv"
>> as.environment(".GlobalEnv")
> <environment: R_GlobalEnv>
>
>>as.environment("R_GlobalEnv")
> Error in as.environment("R_GlobalEnv") :
>   no item called "R_GlobalEnv" on the search list
>> .GlobalEnv
> <environment: R_GlobalEnv>
>
>> environmentName("package:stats")
> [1] ""
>> as.environment("package:stats")
> <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.
>

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".

--
There is nothing more pleasant than traveling and meeting new people!
Genghis Khan

Maranatha! <><
John McKown

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: What the difference between .Golbalenv and package:base?

MacQueen, Don
In reply to this post by PO SU
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" <[hidden email]> 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: [hidden email]
>Majored in Statistics from SJTU
>______________________________________________
>[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: What the difference between .Golbalenv and package:base?

PO SU
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: [hidden email]
Majored in Statistics from SJTU



At 2014-08-26 02:51:54, "MacQueen, Don" <[hidden email]> 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" <[hidden email]> 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: [hidden email]
>>Majored in Statistics from SJTU
>>______________________________________________
>>[hidden email] 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.
>
______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: What the difference between .Golbalenv and package:base?

Jeff Newmiller
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:<[hidden email]>        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 <[hidden email]> 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: [hidden email]
>Majored in Statistics from SJTU
>
>
>
>At 2014-08-26 02:51:54, "MacQueen, Don" <[hidden email]> 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" <[hidden email]> 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: [hidden email]
>>>Majored in Statistics from SJTU
>>>______________________________________________
>>>[hidden email] 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.
>>
>______________________________________________
>[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: What the difference between .Golbalenv and package:base?

William Dunlap
In reply to this post by PO SU
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 <[hidden email]> 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: [hidden email]
> Majored in Statistics from SJTU
>
>
>
> At 2014-08-26 02:51:54, "MacQueen, Don" <[hidden email]> 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" <[hidden email]> 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: [hidden email]
>>>Majored in Statistics from SJTU
>>>______________________________________________
>>>[hidden email] 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.
>>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: What the difference between .Golbalenv and package:base?

PO SU
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: [hidden email]
Majored in Statistics from SJTU



At 2014-08-27 00:02:07, "William Dunlap" <[hidden email]> 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 <[hidden email]> 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: [hidden email]
>> Majored in Statistics from SJTU
>>
>>
>>
>> At 2014-08-26 02:51:54, "MacQueen, Don" <[hidden email]> 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" <[hidden email]> 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: [hidden email]
>>>>Majored in Statistics from SJTU
>>>>______________________________________________
>>>>[hidden email] 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.
>>>
>> ______________________________________________
>> [hidden email] 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]]

______________________________________________
[hidden email] 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.