I really think I'm missing something totally obvious here, but I just can't put my finger on it: how can I combine rapply()
and either as.list()
or eapply()
or both in order to recursively convert a nested environment
into a nested list
.
What I'm actually after is something like as.list(<env>, recursive=TRUE)
and I thought that rapply()
would somehow be usefull in this.
That's the nested environment:
env <- new.env()
env$world <- new.env()
env$world$europe <- new.env()
env$world$europe$germany <- new.env()
env$world$europe$germany$foo <- 1
env$world$europe$germany$bar <- 2
That's the structrue I'd like to end up with:
env.as.list <- list(
world=list(europe=list(germany=list(foo=1, bar=2)))
)
> env.as.list
$world
$world$europe
$world$europe$germany
$world$europe$germany$foo
[1] 1
$world$europe$germany$bar
[1] 2
And rather than building some slow recursive function based on lapply()
, I would like to make use of the fast .Internal()
function in rapply()
.
But as rapply
is meant to work on lists
, of course it will "stop" once it hits an environment
:
> rapply(as.list(env), as.list, how="list")
$world
$world$europe
<environment: 0x000000001748d640>
What's the trick here?