I have some custom classes, for example:
setClass("foo", slots = c(mat = "matrix"))
I want to handle how the list of foo objects is not listed.
mat <- matrix(rnorm(16), 4) foo <- new("foo", mat = mat) unlist(list(foo))
I thought I might have created methods for c (which I thought was used, but maybe wrong) and unlist would solve the problem.
S3 version
S4 version
#' @export setMethod("unlist", signature = "foo", function(x, recursive = TRUE, use.names = TRUE){ print("call foo unlist") })
c Function
#' @export setMethod("c", signature = "foo", function(x, ..., recursive = FALSE){ print("called foo c") })
But I only see a confirmation message when I use c directly:
c(foo) [1] "called foo c"
unlist just returns the same object without a print message
unlist(list(foo)) [[1]] An object of class "foo" Slot "mat": [,1] [,2] [,3] [,4] [1,] 0.6711541 -0.2783441 -0.4707375 -0.23060105 [2,] 0.7408401 0.4076826 2.2757187 -0.48547413 [3,] 1.8640581 0.3610619 -0.4632473 -0.06498348 [4,] -0.5595930 0.6679157 -0.8142456 0.27499963
If I call unlist(foo) , then I get a print message, but I need to apply it in the list of foo objects. Any thoughts on how I can have unlist handle custom classes in a list?
Ultimately, I want the following to return TRUE :
all.equal(unlist(list(foo)), unlist(list(mat)))