From 2f516d2e66d6719bfa84d7d27ced0e141cf1bbcb Mon Sep 17 00:00:00 2001 From: jangorecki Date: Sat, 28 Nov 2015 12:44:59 +0000 Subject: [PATCH] new prettyprint.char argument to print.data.table for easier pretty print, closes #1374 --- R/data.table.R | 9 +++++---- README.md | 2 ++ inst/tests/tests.Rraw | 6 ++++++ 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/R/data.table.R b/R/data.table.R index 5c8c85ac62..e95e44703a 100644 --- a/R/data.table.R +++ b/R/data.table.R @@ -92,6 +92,7 @@ setPackageName("data.table",.global) print.data.table <- function(x, topn=getOption("datatable.print.topn"), # (5) print the top topn and bottom topn rows with '---' inbetween nrows=getOption("datatable.print.nrows"), # (100) under this the whole (small) table is printed, unless topn is provided + prettyprint.char=getOption("datatable.prettyprint.char"), # cut the long string in the char columns row.names = TRUE, quote = FALSE, ...) { if (.global$print!="" && address(x)==.global$print) { # The !="" is to save address() calls and R's global cache of address strings @@ -133,7 +134,7 @@ print.data.table <- function(x, rn = seq_len(nrow(x)) printdots = FALSE } - toprint=format.data.table(toprint, ...) + toprint=format.data.table(toprint, prettyprint.char=prettyprint.char, ...) # fix for #975. if (any(sapply(x, function(col) "integer64" %in% class(col))) && !"package:bit64" %in% search()) { warning("Some columns have been read as type 'integer64' but package bit64 isn't loaded. Those columns will display as strange looking floating point data. There is no need to reload the data. Just require(bit64) to obtain the integer64 print method and print the data again.") @@ -157,7 +158,7 @@ print.data.table <- function(x, # FR #2591 - format.data.table issue with columns of class "formula" is.formula <- function(x) class(x) == "formula" -format.data.table <- function (x, ..., justify="none") { +format.data.table <- function (x, ..., prettyprint.char = getOption("datatable.prettyprint.char"), justify="none") { if (is.atomic(x) && !is.null(x)) { stop("Internal structure doesn't seem to be a list. Possibly corrupt data.table.") } @@ -169,7 +170,7 @@ format.data.table <- function (x, ..., justify="none") { } # FR #1091 for pretty printing of character # TODO: maybe instead of doing "this is...", we could do "this ... test"? - char.trunc <- function(x, trunc.char = getOption("datatable.prettyprint.char")) { + char.trunc <- function(x, trunc.char) { trunc.char = max(0L, suppressWarnings(as.integer(trunc.char[1L])), na.rm=TRUE) if (!is.character(x) || trunc.char <= 0L) return(x) idx = which(nchar(x) > trunc.char) @@ -179,7 +180,7 @@ format.data.table <- function (x, ..., justify="none") { do.call("cbind",lapply(x,function(col,...){ if (!is.null(dim(col))) stop("Invalid column: it has dimensions. Can't format it. If it's the result of data.table(table()), use as.data.table(table()) instead.") if (is.list(col)) col = sapply(col, format.item) - else col = format(char.trunc(col), justify=justify, ...) # added an else here to fix #5435 + else col = format(char.trunc(col, trunc.char = prettyprint.char), justify=justify, ...) # added an else here to fix #5435 col },...)) } diff --git a/README.md b/README.md index 243f2632e5..fa3ab3f997 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,8 @@ 13. GForce is optimised for `.SD[val]` and `col[val]` where `val` is a positive length-1 value. Partly addresses [#523](https://github.com/Rdatatable/data.table/issues/523). + 14. data.table `print` method gets new argument `prettyprint.char` default to `getOption("datatable.prettyprint.char")` which allows easier pretty print without the need to set options. Closes [#1374](https://github.com/Rdatatable/data.table/issues/1374). Thanks @jangorecki. + #### BUG FIXES 1. Now compiles and runs on IBM AIX gcc. Thanks to Vinh Nguyen for investigation and testing, [#1351](https://github.com/Rdatatable/data.table/issues/1351). diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index ccea67459b..faed0ef3eb 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -7217,6 +7217,12 @@ options(datatable.optimize=optim) # handle NULL value correctly #1429 test(1582, uniqueN(NULL), 0L) +# print data.table prettyprint.char argument #1374 +test(1584.1, capture.output(print(data.table(a = "1234567890"))), c(" a","1: 1234567890")) +test(1584.2, capture.output(print(data.table(a = "1234567890"), prettyprint.char = 5)), c(" a","1: 12345...")) +test(1584.3, capture.output(print(data.table(a = rep("1234567890",101)), prettyprint.char = 5)), c(" a", " 1: 12345...", " 2: 12345...", " 3: 12345...", " 4: 12345...", " 5: 12345...", " --- ", " 97: 12345...", " 98: 12345...", " 99: 12345...", "100: 12345...", "101: 12345...")) + + ########################## # TODO: Tests involving GForce functions needs to be run with optimisation level 1 and 2, so that both functions are tested all the time.