Skip to content

Commit

Permalink
add format_list_item.data.frame (#6593)
Browse files Browse the repository at this point in the history
* Closes #6592, nested frames break if any are just 1-column

* typo

* upd tests

* fix typo in test

* style, tweaks

* refine NEWS

* fix link in NEWS

* Add as contributor

---------

Co-authored-by: Bill Evans <Bill@8PawExpress.com>
Co-authored-by: Michael Chirico <chiricom@google.com>
Co-authored-by: Michael Chirico <michaelchirico4@gmail.com>
  • Loading branch information
4 people authored Dec 3, 2024
1 parent f05893e commit d4244e8
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,6 @@ Authors@R: c(
person("Elise", "Maigné", role="ctb"),
person("Vincent", "Rocher", role="ctb"),
person("Vijay", "Lulla", role="ctb"),
person("Aljaž", "Sluga", role="ctb")
person("Aljaž", "Sluga", role="ctb"),
person("Bill", "Evans", role="ctb")
)
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ S3method(format_col, POSIXct)
S3method(format_col, expression)
export(format_list_item)
S3method(format_list_item, default)
S3method(format_list_item, data.frame)

export(fdroplevels, setdroplevels)
S3method(droplevels, data.table)
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ rowwiseDT(
13. `rbindlist(l, use.names=TRUE)` can now handle different encodings for the column names in different entries of `l`, [#5452](https://github.com/Rdatatable/data.table/issues/5452). Thanks to @MEO265 for the report, and Benjamin Schwendinger for the fix.
14. Added a `data.frame` method for `format_list_item()` to fix error printing data.tables with columns containing 1-column data.frames, [#6592](https://github.com/Rdatatable/data.table/issues/6592). Thanks to @r2evans for the bug report and fix.
## NOTES
1. Tests run again when some Suggests packages are missing, [#6411](https://github.com/Rdatatable/data.table/issues/6411). Thanks @aadler for the note and @MichaelChirico for the fix.
Expand Down
5 changes: 5 additions & 0 deletions R/print.data.table.R
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,11 @@ format_list_item.default = function(x, ...) {
}
}

# #6592 -- nested 1-column frames breaks printing
format_list_item.data.frame = function(x, ...) {
paste0("<", class1(x), paste_dims(x), ">")
}

# FR #1091 for pretty printing of character
# TODO: maybe instead of doing "this is...", we could do "this ... test"?
# Current implementation may have issues when dealing with strings that have combinations of full-width and half-width characters,
Expand Down
13 changes: 13 additions & 0 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -20651,3 +20651,16 @@ test(2298.2, rbindlist(list(y,x), use.names=TRUE), data.table("\u00f6"=c(4,2), "
set(y, j="\u00e4", value=NULL)
test(2298.3, rbindlist(list(x,y), use.names=TRUE, fill=TRUE), data.table("\u00e4"=c(1,NA), "\u00f6"=c(2,4), "\u00fc"=c(3,5)))
test(2298.4, rbindlist(list(y,x), use.names=TRUE, fill=TRUE), data.table("\u00f6"=c(4,2), "\u00fc"=c(5,3), "\u00e4"=c(NA,1)))

# #6592: printing nested single-column frames
test(2299.01, format_list_item(data.frame(a=1)), output="<data.frame[1x1]>")
test(2299.02, format_list_item(data.frame(a=1)[0,,drop=FALSE]), output="<data.frame[0x1]>")
test(2299.03, format_list_item(data.frame(a=1)[,0]), output="<data.frame[1x0]>")
test(2299.04, format_list_item(data.frame(a=1, b=2)[0,,drop=FALSE]), output="<data.frame[0x2]>")
test(2299.06, format_list_item(data.table(a=1)), output="<data.table[1x1]>")
test(2299.07, format_list_item(data.table(a=numeric())), output="<data.table[0x1]>")
test(2299.08, format_list_item(data.table()), output="<data.table[0x0]>")
test(2299.09, format_list_item(data.table(a=numeric(), b=numeric())), output="<data.table[0x2]>")
test(2299.10, data.table(a=1), output="a\n1: *1")
test(2299.11, data.table(a=list(data.frame(b=1))), output="a\n1: <data.frame[1x1]>")
test(2299.12, data.table(a=list(data.table(b=1))), output="a\n1: <data.table[1x1]>")

0 comments on commit d4244e8

Please sign in to comment.