Re: [R] function environment

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Sat 03 Jun 2006 - 02:48:40 EST

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

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

______________________________________________
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 03:02:11 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.