Re: [R] function environment

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Sat 03 Jun 2006 - 02:08:38 EST

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



R-help@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 Received on Sat Jun 03 02:21:29 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sat 03 Jun 2006 - 04:10:30 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.