Search code examples
rmagrittr

What environment should I use with %>% mget


I'm collecting some objects by name to save as a list in an RDS:

A = 1
B = 2
mget(c("A","B"))

If I want to pipe...

library(magrittr)
c("A","B") %>% mget                    # nope
c("A","B") %>% mget(env = globalenv()) # ok

But if I'm working inside some environment and I don't want to retype its name...

e = new.env()
e$a = 1
e$b = 2
with(e, {
  # do some stuff, then...
  c("a","b") %>% mget
})

I'm assuming I should type %>% mget(env = something), but can't figure out what (apart from e).


Solution

  • You could probably get away with parent.env(environment()), as in

    with( e, { c("a","b") %>% mget(env=parent.env(environment())) })