当一个 data.frame
其中包含 S4 对象的列表列时,打印是否存在一般问题?还是说我只是不太幸运?
我在使用来自 git2r
包的对象时遇到了这个问题,但维护者 Stefan Widgren 指出了 Matrix
中的一个例子。请注意,如果将对象通过 dplyr::tbl_df()
发送,则可以打印该对象。我知道打印 S4 对象并不提供太多信息;我只是希望不要出错。
更新为稍微更高的目标:能否保留类似于 data.frame
的质量?
library(Matrix)
library(dplyr)
m <- new("dgCMatrix")
isS4(m)
#> [1] TRUE
df <- data.frame(id = 1:2)
df$matrices <- list(m, m)
df
#> Error in prettyNum(.Internal(format(x, trim, digits, nsmall, width, 3L, : first argument must be atomic
tbl_df(df)
#> Source: local data frame [2 x 2]
#>
#> id
#> (int)
#> 1 1
#> 2 2
#> Variables not shown: matrices (list).
## force dplyr to show the tricky column
tbl_df(select(df, matrices))
#> Source: local data frame [2 x 1]
#>
#> matrices
#> (list)
#> 1 <S4:dgCMatrix, CsparseMatrix, dsparseMatrix, generalMatrix, dCsparseMatrix,
#> 2 <S4:dgCMatrix, CsparseMatrix, dsparseMatrix, generalMatrix, dCsparseMatrix,
## rawr points out that this does not error ... but loses the df quality
print.default(df)
#> $id
#> [1] 1 2
#>
#> $matrices
#> $matrices[[1]]
#> 0 x 0 sparse Matrix of class "dgCMatrix"
#> <0 x 0 matrix>
#>
#> $matrices[[2]]
#> 0 x 0 sparse Matrix of class "dgCMatrix"
#> <0 x 0 matrix>
#>
#>
#> attr(,"class")
#> [1] "data.frame"