Re: [Rd] Create an environment and assign objects to it in one go?

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Thu, 10 Mar 2011 08:53:53 -0500

On 3/10/11, Henrik Bengtsson <hb_at_biostat.ucsf.edu> wrote:
> Hi,
>
> I've just created:
>
> newEnvEval <- function(..., hash=FALSE, parent=parent.frame(), size=29L) {
> envir <- new.env(hash=hash, parent=parent, size=size);
> evalq(..., envir=envir);
> envir;
> } # newEnvEval()
>
> so that I can create an environment and assign objects to it in one go, e.g.
>
> env <- newEnvEval({ a <- 1; b <- 2; });
> print(env$a);
>
> Does this already exists somewhere?
>
> /Henrik
>

You can do this:

   e <- as.environment(list(a = 1, b = 2))    e$a; e$b

or since proto objects are environments (but the $ has slightly different semantics when applied to functions):

   library(proto)
   p <- proto(a = 1, b = 2, expr = { c <- 3 })
   p$a; p$b; p$c

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Thu 10 Mar 2011 - 13:56:47 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 10 Mar 2011 - 18:00:28 GMT.

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

list of date sections of archive