Henrik Bengtsson
2011-Mar-10 07:44 UTC
[Rd] Create an environment and assign objects to it in one go?
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 PS.
peter dalgaard
2011-Mar-10 07:53 UTC
[Rd] Create an environment and assign objects to it in one go?
On Mar 10, 2011, at 08:44 , Henrik Bengtsson 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? >I think env <- local({ a <- 1; b <- 2; environment()}) env$a -pd> /Henrik > > PS. > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel-- Peter Dalgaard Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
Gabor Grothendieck
2011-Mar-10 13:53 UTC
[Rd] Create an environment and assign objects to it in one go?
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
Possibly Parallel Threads
- Inspect a "delayed" assigned whose value throws an error?
- bquote/evalq behavior changed in R-3.2.1
- best practice(s) for retrieving a local variable from a closure
- bquote/evalq behavior changed in R-3.2.1
- WISH: eval() to preserve the "visibility" (now value is always visible)