From hb at biostat.ucsf.edu  Thu Mar 10 18:21:55 2011
From: hb at biostat.ucsf.edu (Henrik Bengtsson)
Date: Thu, 10 Mar 2011 09:21:55 -0800
Subject: [Rd] Create an environment and assign objects to it in one go?
In-Reply-To: <AANLkTi=F5kYczLggA=4SFT8Qo87UQuWTO67dpV6u5y7z@mail.gmail.com>
References: <AANLkTi=n9v0ebCbcsqtZCGQEtUa2nN_rUpoM+0uf0shS@mail.gmail.com>
	<AANLkTi=F5kYczLggA=4SFT8Qo87UQuWTO67dpV6u5y7z@mail.gmail.com>
Message-ID: <AANLkTincWaugmuSDYUajjieaOLWDRmAhrtg7vHmBoaqH@mail.gmail.com>

Hi,

thanks to both of you.  My use case was actually to in-the-end create
a *list* in a cut'n'paste-friendly way, e.g.

env <- function(..., hash=FALSE, parent=parent.frame(), size=29L) {
  envir <- new.env(hash=hash, parent=parent, size=size);
  evalq(..., envir=envir);
? envir;
} # env()


x <- list();

x$case1 <- env({
  # Cut'n'pasted from elsewhere
  a <- 1;
  b <- 2;
});

x$case2 <- env({
  # Cut'n'pasted from elsewhere
  a <- 3;
  b <- 1;
});

x <- lapply(x, FUN=as.list);

> str(x)
List of 2
 $ case1:List of 2
  ..$ b: num 2
  ..$ a: num 1
 $ case2:List of 2
  ..$ b: num 1
  ..$ a: num 3

/Henrik


On Thu, Mar 10, 2011 at 5:53 AM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
> 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
>



[Rd] Create an environment and assign objects to it in one go?
Henrik Bengtsson