-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, how can I automatically access the functions that I loaded into a separate environment?> save(A,B,file="myfun.r") > load("myfun.r",envir=(ENV<-new.env())) > ls(ENV)[1] "A" "B" ?"[" turned up that I can access the functions via> ENV$Afunction () { }> ENV$A()NULL Now, how can they be included in the search() path?? attach() as for data.frames does not work... Furthermore, if I change a functions environment to ENV, why is it not listed with ls(ENV)?? Instead it still lives in .GlobalEnv> C<-function(){} > environment(C)<-ENV > ls(ENV)[1] "A" "B"> Cfunction(){} <environment: 0x9cbbb58>> ENV<environment: 0x9cbbb58> Thanks folks! I enjoy reading and learning from r-help list! M -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2.2 (GNU/Linux) iD8DBQFEgFw2XjamRUP82DkRAooRAJ9sxwERwfXF3l7pssZ081sMC1+nigCgqAPM OkA1tNJg6MN3l0PQFrwBlIE=tGFq -----END PGP SIGNATURE-----
Try this: attach(as.list(ENV)) search() # shows it on the path A function's environment refers to where variables in the function are looked for if they can't find the variable in the function itself. The environment in which a function itself is located is entirely different. e.g. a <- 1 e <- new.env() e$a <- 2 e$fun <- function(x) x*x+a environment(fun) e$fun(10) # 101 fun2 <- function(x) x*x + a environment(fun2) <- e fun2(10) # 102 fun is located in environment e but the environment of fun, i.e. environment(fun), is the .GlobalEnv. fun2 is located in the .GlobalEnv but the environment of fun2, i.e. environment(fun2) is e. On 6/2/06, Matthias Braeunig <mb.atelier at web.de> wrote:> -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Hi, > > how can I automatically access the functions that I loaded into a > separate environment? > > > save(A,B,file="myfun.r") > > load("myfun.r",envir=(ENV<-new.env())) > > ls(ENV) > [1] "A" "B" > > ?"[" turned up that I can access the functions via > > > ENV$A > function () > { > } > > ENV$A() > NULL > > Now, how can they be included in the search() path?? > attach() as for data.frames does not work... > > Furthermore, if I change a functions environment to ENV, why is it not > listed with ls(ENV)?? Instead it still lives in .GlobalEnv > > > C<-function(){} > > environment(C)<-ENV > > ls(ENV) > [1] "A" "B" > > C > function(){} > <environment: 0x9cbbb58> > > ENV > <environment: 0x9cbbb58> > > Thanks folks! > I enjoy reading and learning from r-help list! > > M > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.2.2 (GNU/Linux) > > iD8DBQFEgFw2XjamRUP82DkRAooRAJ9sxwERwfXF3l7pssZ081sMC1+nigCgqAPM > OkA1tNJg6MN3l0PQFrwBlIE> =tGFq > -----END PGP SIGNATURE----- > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
On Fri, 2 Jun 2006, Matthias Braeunig wrote:> -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Hi, > > how can I automatically access the functions that I loaded into a > separate environment?This is a topic that is probably best suited to R-devel: the explanations are going to get technical, and I am not being comprehensive here.>> save(A,B,file="myfun.r") >> load("myfun.r",envir=(ENV<-new.env())) >> ls(ENV) > [1] "A" "B" > > ?"[" turned up that I can access the functions via > >> ENV$A > function () > { > } >> ENV$A() > NULL > > Now, how can they be included in the search() path?? > attach() as for data.frames does not work...Actually, it does. attach() for a list creates an environment and copies the elements of a list to it. Try attach("myfun.r", pos=2) or more explicitly env <- attach(NULL pos=2, name="myenv") load("myfun.r", envir=env) which both create an environment and unserialize the objects/bindings into it. We have discussed allowing an environment to be given as an argument of attach, but the semantics are not clear: should the environment itself be attached or a copy (as for a list). It seems that a copy would be most natural. Since loading a package clearly does load objects into an environment on the search path, there are other sneaky games you can play if you know what you are doing.> Furthermore, if I change a functions environment to ENV, why is it not > listed with ls(ENV)?? Instead it still lives in .GlobalEnvBecause that sets the enclosing environment, not the frame in which the symbol is resolved. There are at least three possible sources of confusion here: 1) `environment' gets used in two senses in R documentation, one as a frame and one as an environment tree, that is as a frame and its enclosure (and so on recursively). 2) Note that objects do not really `live' in environments: symbols in frames have values and an object can be the value of several symbols. (People talk about bindings here: there are symbols are bound to objects, but objects can be bound to multiple symbols.) 3) Closures (most functions, including all user-written functions) have formals, a body, and an environment tree `attached to' or `associated with' the function. This is called the 'environment' of the function, but the 'enclosure' might be less confusing (since it acts as the enclosure of the frame created for the body of the function when it is evaluated). -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Try save(A, B, file = "myfun.r") attach("myfun.r") Your functions will be on the search list. -roger Matthias Braeunig wrote:> -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Hi, > > how can I automatically access the functions that I loaded into a > separate environment? > >> save(A,B,file="myfun.r") >> load("myfun.r",envir=(ENV<-new.env())) >> ls(ENV) > [1] "A" "B" > > ?"[" turned up that I can access the functions via > >> ENV$A > function () > { > } >> ENV$A() > NULL > > Now, how can they be included in the search() path?? > attach() as for data.frames does not work... > > Furthermore, if I change a functions environment to ENV, why is it not > listed with ls(ENV)?? Instead it still lives in .GlobalEnv > >> C<-function(){} >> environment(C)<-ENV >> ls(ENV) > [1] "A" "B" >> C > function(){} > <environment: 0x9cbbb58> >> ENV > <environment: 0x9cbbb58> > > Thanks folks! > I enjoy reading and learning from r-help list! > > M > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.2.2 (GNU/Linux) > > iD8DBQFEgFw2XjamRUP82DkRAooRAJ9sxwERwfXF3l7pssZ081sMC1+nigCgqAPM > OkA1tNJg6MN3l0PQFrwBlIE> =tGFq > -----END PGP SIGNATURE----- > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >-- Roger D. Peng | http://www.biostat.jhsph.edu/~rpeng/
>>>>> "Roger" == Roger D Peng <rdpeng at gmail.com> >>>>> on Fri, 02 Jun 2006 13:03:12 -0400 writes:Roger> Try Roger> save(A, B, file = "myfun.r") Roger> attach("myfun.r") Roger> Your functions will be on the search list. yes. But do yourself (and your readers/users/..) a favor by using a different file extension: ".r" is used in place of ".R" (mainly by people who work on (non-)operating systems that do not properly differentiate between lower and upper case) for R source files. Files resulting from save() typically get an extension ".rda" (or also ".Rdata"). Martin Roger> -roger Roger> Matthias Braeunig wrote: >> -----BEGIN PGP SIGNED MESSAGE----- >> Hash: SHA1 >> >> Hi, >> >> how can I automatically access the functions that I loaded into a >> separate environment? >> >>> save(A,B,file="myfun.r") >>> load("myfun.r",envir=(ENV<-new.env())) >>> ls(ENV) >> [1] "A" "B" >> >> ?"[" turned up that I can access the functions via >> >>> ENV$A >> function () >> { >> } >>> ENV$A() >> NULL >> >> Now, how can they be included in the search() path?? >> attach() as for data.frames does not work... >> >> Furthermore, if I change a functions environment to ENV, why is it not >> listed with ls(ENV)?? Instead it still lives in .GlobalEnv >> >>> C<-function(){} >>> environment(C)<-ENV >>> ls(ENV) >> [1] "A" "B" >>> C >> function(){} >> <environment: 0x9cbbb58> >>> ENV >> <environment: 0x9cbbb58> >> >> Thanks folks! >> I enjoy reading and learning from r-help list! >> >> M >> -----BEGIN PGP SIGNATURE----- >> Version: GnuPG v1.4.2.2 (GNU/Linux) >> >> iD8DBQFEgFw2XjamRUP82DkRAooRAJ9sxwERwfXF3l7pssZ081sMC1+nigCgqAPM >> OkA1tNJg6MN3l0PQFrwBlIE >> =tGFq >> -----END PGP SIGNATURE----- >> >> ______________________________________________ >> R-help at stat.math.ethz.ch mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >> Roger> -- Roger> Roger D. Peng | http://www.biostat.jhsph.edu/~rpeng/ Roger> ______________________________________________ Roger> R-help at stat.math.ethz.ch mailing list Roger> https://stat.ethz.ch/mailman/listinfo/r-help Roger> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html