Assume this simplified example:
L <- list()
L$Foo <- list()
L$Foo$Bar <- list()
L$Foo$Bar$X <- TRUE
L$Foo$Bar$Y <- "abc"
L$Lorem <- list()
L$Lorem$Ipsum <- list()
L$Lorem$Ipsum$Dolor <- list()
L$Lorem$Ipsum$Dolor$X <- TRUE
L$Lorem$Ipsum$Dolor$Z <- "xyz"
In this question, I attempted to recast a nested list of any depth after unlisting it. One answer suggested using unlist()
and then relist()
.
However, this suggestion does not preserve the original classes if they vary. For instance, relist(unlist(L), L)
will return:
$Foo
$Foo$Bar
$Foo$Bar$X
[1] "TRUE"
$Foo$Bar$Y
[1] "abc"
$Lorem
$Lorem$Ipsum
$Lorem$Ipsum$Dolor
$Lorem$Ipsum$Dolor$X
[1] "TRUE"
$Lorem$Ipsum$Dolor$Z
[1] "xyz"
Notice that "TRUE"
and "FALSE"
are erroneously characters, not logicals.
Now, one solution is provided here. However, the answer from this question does not work for any depth level of the nested list. For instance, running relist2(flatten(L), L)
as suggested from the answer returns:
> relist2(flatten(L), L)
$Foo
$Foo$Bar.X
$Foo$Bar.X[[1]]
[1] TRUE
$Foo$Bar.Y
$Foo$Bar.Y[[1]]
[1] "abc"
$Lorem
$Lorem$Ipsum.Dolor
$Lorem$Ipsum.Dolor$X
$Lorem$Ipsum.Dolor$X[[1]]
[1] TRUE
$Lorem$Ipsum.Dolor$Z
$Lorem$Ipsum.Dolor$Z[[1]]
[1] "xyz"
Here, the classes are preserved, but not all levels are relisted—notice Ipsum.Dolor
that should have been relisted as Ipsum$Dolor
.
Any thoughts on how to solve this?