Skip to content
Prev 24991 / 63424 Next

relist, an inverse operator to unlist

unlist would not attach a skeleton to every vector it returns, only
the relist method of unlist would.   That way just that method needs
to be added and no changes to unlist itself are needed.

Before applying unlist to an object you would coerce the object to
class "relist" to force the relist method of unlist to be invoked.

Here is an outline of the code:

as.relist <- function(x) {
   if (!inherits(x, "relist")) class(x) <- c("relist", class(x))
   x
}

unlist.relist <- function(x, ...) {
   y <- x
   cl <- class(y)
   class(y) <- cl[- grep("relist", cl)]
   z <- unlist(y)
   attr(z, "relist") <- y
   as.relist(z)
}

relist <- function(x, skeleton = attr(x, "relist")) {
   # simpler version of relist so test can be executed
   skeleton
}

# test
x <- list(a = 1:2, b = 3)
class(as.relist(x))
unlist(as.relist(x))
relist(unlist(as.relist(x)))
On 5/14/07, Andrew Clausen <clausen at econ.upenn.edu> wrote:

        
On 5/14/07, Andrew Clausen <clausen at econ.upenn.edu> wrote: