Skip to content

Commit

Permalink
Closes #3011 -- adds a method to print expression columns
Browse files Browse the repository at this point in the history
reword
  • Loading branch information
MichaelChirico committed May 4, 2019
1 parent 1d583f5 commit bf591d6
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,6 @@ S3method(unique, ITime)
export(format_col)
S3method(format_col, default)
S3method(format_col, POSIXct)
S3method(format_col, expression)
export(format_list_item)
S3method(format_list_item, default)
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

6. New variable `.Last.updated` (similar to R's `.Last.value`) contains the number of rows affected by the most recent `:=` or `set()`, [#1885](https://github.com/Rdatatable/data.table/issues/1885).
7. `data.table` printing now supports customizable methods for both columns and list column row items, part of [#1523](https://github.com/Rdatatable/data.table/issues/1523). `format_col` is S3-generic for customizing how to print whole columns; `format_list_item` is S3-generic for customizing how to print each row of a list column. Thanks variously to @mllg, who initially filed [#3338](https://github.com/Rdatatable/data.table/pulls/3338) with the seed of the idea, @franknarf1 who earlier suggested the idea of providing custom formatters, @fparages who submitted a patch to improve the printing of timezones for [#2842](https://github.com/Rdatatable/data.table/issues/2842), and @MichaelChirico for the ultimate implementation. See `?print.data.table` for examples.
7. `data.table` printing now supports customizable methods for both columns and list column row items, part of [#1523](https://github.com/Rdatatable/data.table/issues/1523). `format_col` is S3-generic for customizing how to print whole columns; `format_list_item` is S3-generic for customizing how to print each row of a list column. Thanks variously to @mllg, who initially filed [#3338](https://github.com/Rdatatable/data.table/pulls/3338) with the seed of the idea, @franknarf1 who earlier suggested the idea of providing custom formatters, @fparages who submitted a patch to improve the printing of timezones for [#2842](https://github.com/Rdatatable/data.table/issues/2842), @RichardRedding for pointing out an error relating to printing wide `expression` columns in [#3011](https://github.com/Rdatatable/data.table/issues/3011), and @MichaelChirico for the ultimate implementation. See `?print.data.table` for examples.
#### BUG FIXES
Expand Down
3 changes: 3 additions & 0 deletions R/print.data.table.R
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ format_col.default = function(x, ...) {
# #2842 -- different columns can have different tzone, so force usage in output
format_col.POSIXct = function(x, ...) format(x, usetz = TRUE, ...)

# #3011 -- expression columns can wrap to newlines which breaks printing
format_col.expression = function(x, ...) format(char.trunc(as.character(x)), ...)

format_list_item.default = function(x, ...) {
if (is.null(x)) return ("") # NULL item in a list column
if (is.atomic(x) || inherits(x, "formula")) # FR #2591 - format.data.table issue with columns of class "formula"
Expand Down
9 changes: 6 additions & 3 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -14141,19 +14141,22 @@ DT = data.table(
t1 = as.POSIXct('2018-05-01 12:34:56', tz = 'UTC'),
t2 = as.POSIXct('2018-05-01 12:34:56', tz = 'Asia/Singapore')
)
test(1992.1, DT, output = 'UTC.*\\+08')
test(2032.1, DT, output = 'UTC.*\\+08')

# #3011 -- default expression printing can break format_col.default
test(2032.2, print(data.table(e = expression(1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13))), output = '1 + 2 + 3')

# format_col generic is used
format_col.complex = function(x, ...) sprintf('(%.1f, %.1fi)', Re(x), Im(x))
registerS3method("format_col", "complex", format_col.complex)
x = data.table(z = c(1 + 3i, 2 - 1i, pi + 2.718i))
test(1992.2, x, output = '(1.0, 3.0i)')
test(2032.3, x, output = '(1.0, 3.0i)')

# format_list_item() generic is used
format_list_item.myclass <- function(x, ...) paste0("<", class(x)[1L], ":", x$id, ">")
registerS3method("format_list_item", "myclass", format_list_item.myclass)
DT = data.table(row = 1:2, objs = list(structure(list(id = "foo"), class = "myclass"), structure(list(id = "bar"), class = "myclass")))
test(1992.3, print(DT), output = "myclass:foo.*myclass:bar")
test(2032.4, print(DT), output = "myclass:foo.*myclass:bar")

###################################
# Add new tests above this line #
Expand Down
1 change: 1 addition & 0 deletions man/print.data.table.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
format_col(x, \dots)
\method{format_col}{default}(x, \dots)
\method{format_col}{POSIXct}(x, \dots)
\method{format_col}{expression}{x, \dots)

format_list_item(x, \dots)
\method{format_list_item}{default}(x, \dots)
Expand Down

0 comments on commit bf591d6

Please sign in to comment.