【发布时间】:2016-04-16 08:47:20
【问题描述】:
当data.frame 具有 S4 对象的列表列时,打印它是否存在一般问题?还是我运气不好?
我在 git2r 包中遇到了这个问题,但维护者 Stefan Widgren 也从 Matrix 中指出了这个例子。我注意到如果通过dplyr::tbl_df() 发送对象,则可以打印该对象。我接受印刷没有提供太多关于 S4 对象的信息; 我只要求没有错误。
更新的野心略高:data.frame-like 的品质能否保留?
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"
【问题讨论】: