From bolker at zoo.ufl.edu  Fri Mar  7 20:02:02 2008
From: bolker at zoo.ufl.edu (Ben Bolker)
Date: Fri, 07 Mar 2008 14:02:02 -0500
Subject: [Rd] merging environments
Message-ID: <47D1912A.3010802@zoo.ufl.edu>


   Despite the spirited arguments of various R-core folks
who feel that mle() doesn't need a "data" argument, and
that users would be better off learning to deal with function
closures, I am *still* trying to make such things work
in a reasonably smooth fashion ...

   Is there a standard idiom for "merging" environments?
i.e., suppose a function has an environment that I want
to preserve, but _add_ the contents of a data list --
would something like this do it? Is there a less ugly
way?

x <- 0
y <- 1
z <- 2

f <- function() {
     x+y+z
}

f2 <- function(fun,data) {
     L <- ls(pos=environment(fun))
     mapply(assign,names(data),data,
                      MoreArgs=list(envir=environment(fun)))
     print(ls(pos=environment(fun)))
}

f2(f,list(a=1))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 252 bytes
Desc: OpenPGP digital signature
Url : https://stat.ethz.ch/pipermail/r-devel/attachments/20080307/beda69c0/attachment.bin 


[Rd] merging environments
Ben Bolker