Czy występuje ogólny problem z drukowaniem data.frame
, gdy ma on kolumnę listy obiektów S4? Czy mam po prostu pecha?drukowanie elementu data.frame zawierającego kolumnę listy obiektów S4
Przebiegłem to z obiektami z pakietu git2r
, ale opiekun Stefan Widgren również wskazuje ten przykład z Matrix
. Zauważam, że obiekt można wydrukować, jeśli zostanie wysłany przez dplyr::tbl_df()
. Akceptuję, że druk nie dostarcza zbyt wielu informacji o obiektach S4;
wszystko, o co pytam, to nie błąd
.
AKTUALIZACJA z nieco większą ambicją: Czy można zachować jakość podobną do 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"
Nie nazwałbym że „obejścia”. To niszcząco wymusza elementy w drugiej kolumnie na zupełnie inną klasę. Kolumny ramek danych nie powinny zawierać list. Podejrzewam, że oni również powinni zawierać konstruktorów S4. Klasyczne, podatne na błędy działania obejmują POSIXlt sklasyfikowane wektory. Wiem, że nie powinny zawierać obiektów języka, takich jak funkcje lub wyrażenia R. Myślę, że powinieneś zrezygnować z ramek danych jako celu i użyć innej klasy kontenera. –