Skip to content

Commit 8809fc9

Browse files
authored
use list2DF to create a list-column data.frame (#169)
* use list2DF to create a list-column data.frame * add tests for data frames with list columns
1 parent 49e8b23 commit 8809fc9

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

R/repr_matrix_df.r

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ arr_part_unpack_tbl <- function(tbl) {
7070
names(res) <- paste0(prefix, '$', names(res))
7171
return(res)
7272
} else {
73-
res <- data.frame(col)
73+
if(is.list(col)) {
74+
res <- list2DF(list(col))
75+
} else {
76+
res <- data.frame(col)
77+
}
7478
names(res) <- prefix
7579
return(res)
7680
}

tests/testthat/test_repr_array_df.r

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
options(stringsAsFactors = FALSE)
22

3+
has_dt <- requireNamespace('data.table', quietly = TRUE)
4+
has_tibble <- requireNamespace('tibble', quietly = TRUE)
35

46
test_that('empty data.frames work', {
57
expect_identical(repr_html(data.frame()), '')
@@ -180,3 +182,25 @@ test_that('nested data.frames can be displayed', {
180182
repr_text(outer)
181183
succeed()
182184
})
185+
186+
test_that('data.frame with list columns can be displayed', {
187+
df <- list2DF(list(a=1, b=list(1:2)))
188+
expected <- '<table class="dataframe">
189+
<caption>A data.frame: 1 × 2</caption>
190+
<thead>
191+
\t<tr><th scope=col>a</th><th scope=col>b</th></tr>
192+
\t<tr><th scope=col>&lt;dbl&gt;</th><th scope=col>&lt;list&gt;</th></tr>
193+
</thead>
194+
<tbody>
195+
\t<tr><td>1</td><td>1, 2</td></tr>
196+
</tbody>
197+
</table>
198+
'
199+
expect_identical(repr_html(df), expected)
200+
if(has_tibble) {
201+
expect_identical(repr_html(tibble::as_tibble(df)), sub('data\\.frame','tibble',expected))
202+
}
203+
if(has_dt) {
204+
expect_identical(repr_html(data.table::as.data.table(df)), sub('data\\.frame','data.table',expected))
205+
}
206+
})

0 commit comments

Comments
 (0)