What the difference between .Golbalenv and package:base?
On Mon, Aug 25, 2014 at 12:26 PM, John McKown
<john.archie.mckown at gmail.com> 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