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: desolato...@163.com Majored in Statistics from SJTU At 2014-08-26 01:26:28, "John McKown" <john.archie.mck...@gmail.com> wrote: >On Mon, Aug 25, 2014 at 11:19 AM, PO SU <rhelpmaill...@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: desolato...@163.com >> Majored in Statistics from SJTU > > >-- >There is nothing more pleasant than traveling and meeting new people! >Genghis Khan > >Maranatha! <>< >John McKown ______________________________________________ R-help@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.