[R] assigning to a list in a package environment

Gabor Grothendieck ggrothendieck at gmail.com
Thu May 12 16:46:02 CEST 2005


I did not fully understand which environment you want but:

1. if this is written in your function:  e <- environment()
    then e contains the current environment in the function

2. parent.env(e)   is the lexical environment within which
    your function was defined.  Also, if the function is called
    f then environment(f) gives this too.

3. parent.frame() is the environment from which your 
    function was called.

On 5/12/05, Sean Davis <sdavis2 at mail.nih.gov> wrote:
> 
> On May 12, 2005, at 10:09 AM, Gabor Grothendieck wrote:
> 
> > On 5/11/05, Sean Davis <sdavis2 at mail.nih.gov> wrote:
> >> I have a list in a package environment
> >>
> >> assign('refflat',list(),pos='package:locPkg')
> >>
> >> to which I would like to make assignments like:
> >>
> >> refflat[['a']] <- read.table('fileA.txt')
> >> refflat[['b']] <- read.table('fileB.txt')
> >>
> >> I am doing this to guard against a local version of refflat hanging
> >> around, as I want to refresh it with each new session (and so, want to
> >> store it in the package environment).  I just can't quite get hot to
> >> make that work so that I am storing to the package:refflat rather than
> >> any in .GlobalEnv.
> >
> > Try this:
> >
> > locPkg <- as.environment("package:locPkg")
> > locPkg$refflat <- list()
> > locPkg$refflat[["a"]] <- read.table("fileA.txt")
> >
> 
> Gabor,
> 
> Thanks.  That will do the trick, I think.  Now, just one more question
> as followup if I may?  If I am executing a function that was defined in
> locPkg, how can I determine dynamically what environment I am in?  Them
> I could do something like
> 
> myFun <- function (x) {
>   # code
>   locPkg <- as.environment( .... )
>   # more code
> }
> 
> where '....' is replaced dynamically by the package environment of the
> package in which the function myFun is defined.  The goal is to make
> the code a bit more portable.
> 
> Again, thanks for the help.
> 
> Sean
> 
>




More information about the R-help mailing list