From 286c849b416901a26d8dc1f3b0fd59d9ea4fa966 Mon Sep 17 00:00:00 2001 From: joshhwuu Date: Fri, 12 Apr 2024 11:40:12 -0700 Subject: [PATCH 1/8] Added naprint argument to print.data.table --- R/print.data.table.R | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/R/print.data.table.R b/R/print.data.table.R index dd641f946..7f351fd8d 100644 --- a/R/print.data.table.R +++ b/R/print.data.table.R @@ -8,6 +8,7 @@ print.data.table = function(x, topn=getOption("datatable.print.topn"), print.keys=getOption("datatable.print.keys"), trunc.cols=getOption("datatable.print.trunc.cols"), quote=FALSE, + na.print=NULL, timezone=FALSE, ...) { # topn - print the top topn and bottom topn rows with '---' inbetween (5) # nrows - under this the whole (small) table is printed, unless topn is provided (100) @@ -118,9 +119,9 @@ print.data.table = function(x, topn=getOption("datatable.print.topn"), } rownames(toprint) = format(rownames(toprint), justify="right") if (col.names == "none") { - cut_colnames(print(toprint, right=TRUE, quote=quote)) + cut_colnames(print(toprint, right=TRUE, quote=quote, na.print=na.print)) } else { - print(toprint, right=TRUE, quote=quote) + print(toprint, right=TRUE, quote=quote, na.print=na.print) } if (trunc.cols && length(not_printed) > 0L) # prints names of variables not shown in the print @@ -133,9 +134,9 @@ print.data.table = function(x, topn=getOption("datatable.print.topn"), # option to shut this off per request of Oleg Bondar on SO, #1482 toprint=rbind(toprint, matrix(if (quote) old else colnames(toprint), nrow=1L)) # fixes bug #97 if (col.names == "none") { - cut_colnames(print(toprint, right=TRUE, quote=quote)) + cut_colnames(print(toprint, right=TRUE, quote=quote, na.print=na.print)) } else { - print(toprint, right=TRUE, quote=quote) + print(toprint, right=TRUE, quote=quote, na.print=na.print) } if (trunc.cols && length(not_printed) > 0L) # prints names of variables not shown in the print From 48d53a4b051b9460c0cb1a2f8b5909f92060c483 Mon Sep 17 00:00:00 2001 From: joshhwuu Date: Fri, 12 Apr 2024 11:40:34 -0700 Subject: [PATCH 2/8] Added corresponding documentation --- man/print.data.table.Rd | 2 ++ 1 file changed, 2 insertions(+) diff --git a/man/print.data.table.Rd b/man/print.data.table.Rd index a39c8c446..4d549b629 100644 --- a/man/print.data.table.Rd +++ b/man/print.data.table.Rd @@ -26,6 +26,7 @@ print.keys=getOption("datatable.print.keys"), # default: TRUE trunc.cols=getOption("datatable.print.trunc.cols"), # default: FALSE quote=FALSE, + na.print=NULL, timezone=FALSE, \dots) format_col(x, \dots) @@ -47,6 +48,7 @@ \item{trunc.cols}{ If \code{TRUE}, only the columns that can be printed in the console without wrapping the columns to new lines will be printed (similar to \code{tibbles}). } \item{quote}{ If \code{TRUE}, all output will appear in quotes, as in \code{print.default}. } \item{timezone}{ If \code{TRUE}, time columns of class POSIXct or POSIXlt will be printed with their timezones (if attribute is available). } + \item{na.print}{ The string to be printed in place of \code{NA} values. } \item{\dots}{ Other arguments ultimately passed to \code{format}. } } \value{ From 016cab21b78f1c0600f8d0e7d03b739c9fe80882 Mon Sep 17 00:00:00 2001 From: joshhwuu Date: Fri, 12 Apr 2024 11:40:40 -0700 Subject: [PATCH 3/8] Simple tests --- inst/tests/tests.Rraw | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index d33bd72a6..41b0e9446 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -18454,3 +18454,9 @@ test(2256.6, fread('a;b\n1,14;5', verbose=TRUE), data.table(a=1.14, b=5L), outpu # helpful error about deleting during grouping, #1873 DT = data.table(id = c(1, 1, 2, 2), a = 1:4, b = 5:8) test(2257, DT[ , c("c", "a") := .(a + 1, NULL), by=id], error="it's not possible to delete parts of a column") + +DT = data.table(x = c(NA, "a", "b")) +test(2258.1, capture.output(print(DT, na.print = ".")), c(" x", "1: .", "2: a", "3: b")) +test(2258.2, capture.output(print(DT, na.print = "_")), c(" x", "1: _", "2: a", "3: b")) +test(2258.3, capture.output(print(DT, na.print = "NA")), c(" x", "1: NA", "2: a", "3: b")) +test(2258.4, capture.output(print(DT, na.print = TRUE)), error="invalid 'na.print' specification") \ No newline at end of file From 755f4a981e4ddd8f6a9e713d6f48a9196f056c10 Mon Sep 17 00:00:00 2001 From: joshhwuu Date: Fri, 12 Apr 2024 14:32:12 -0700 Subject: [PATCH 4/8] changed tests, added for when quote=true --- inst/tests/tests.Rraw | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 41b0e9446..4cdaaaa0f 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -18455,8 +18455,9 @@ test(2256.6, fread('a;b\n1,14;5', verbose=TRUE), data.table(a=1.14, b=5L), outpu DT = data.table(id = c(1, 1, 2, 2), a = 1:4, b = 5:8) test(2257, DT[ , c("c", "a") := .(a + 1, NULL), by=id], error="it's not possible to delete parts of a column") -DT = data.table(x = c(NA, "a", "b")) -test(2258.1, capture.output(print(DT, na.print = ".")), c(" x", "1: .", "2: a", "3: b")) -test(2258.2, capture.output(print(DT, na.print = "_")), c(" x", "1: _", "2: a", "3: b")) -test(2258.3, capture.output(print(DT, na.print = "NA")), c(" x", "1: NA", "2: a", "3: b")) -test(2258.4, capture.output(print(DT, na.print = TRUE)), error="invalid 'na.print' specification") \ No newline at end of file +DT = data.table(x=c(NA, "a", "b")) +test(2258.1, capture.output(print(DT, na.print=".")), c(" x", "1: .", "2: a", "3: b")) +test(2258.2, capture.output(print(DT, na.print="_")), c(" x", "1: _", "2: a", "3: b")) +test(2258.3, capture.output(print(DT, na.print="NA")), c(" x", "1: NA", "2: a", "3: b")) +test(2258.4, capture.output(print(DT, na.print=TRUE)), error="invalid 'na.print' specification") +test(2258.5, capture.output(print(DT, na.print=".", quote=TRUE)), c(' "x"', "1: .", '2: "a"', '3: "b"')) \ No newline at end of file From dc3dfb99a708481159db141b35a8399a4faf3f5d Mon Sep 17 00:00:00 2001 From: Joshua Wu Date: Fri, 12 Apr 2024 14:32:48 -0700 Subject: [PATCH 5/8] Update man/print.data.table.Rd Co-authored-by: Michael Chirico --- man/print.data.table.Rd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/man/print.data.table.Rd b/man/print.data.table.Rd index 4d549b629..f740de9d9 100644 --- a/man/print.data.table.Rd +++ b/man/print.data.table.Rd @@ -48,7 +48,7 @@ \item{trunc.cols}{ If \code{TRUE}, only the columns that can be printed in the console without wrapping the columns to new lines will be printed (similar to \code{tibbles}). } \item{quote}{ If \code{TRUE}, all output will appear in quotes, as in \code{print.default}. } \item{timezone}{ If \code{TRUE}, time columns of class POSIXct or POSIXlt will be printed with their timezones (if attribute is available). } - \item{na.print}{ The string to be printed in place of \code{NA} values. } + \item{na.print}{ The string to be printed in place of \code{NA} values, as in \code{print.default}. } \item{\dots}{ Other arguments ultimately passed to \code{format}. } } \value{ From 89a85b712411b51850d796f713e12e94df76a593 Mon Sep 17 00:00:00 2001 From: joshhwuu Date: Fri, 12 Apr 2024 14:35:44 -0700 Subject: [PATCH 6/8] updated NEWS.md --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index 27c35e385..df54be526 100644 --- a/NEWS.md +++ b/NEWS.md @@ -74,6 +74,8 @@ 11. Using `print.data.table` when truncation is needed with `row.names = FALSE` prints the indicator `---` in every value column instead of adding a blank column where the `rownames` would have been just to include `---`, [#4083](https://github.com/Rdatatable/data.table/issues/4083). Thanks @MichaelChirico for the report and @joshhwuu for the fix. +12. `print.data.table` now honors `na.print`, as seen in `print.default`, allowing for string replacement of `NA` values when printing. Thanks @HughParsonage for the report and @joshhwuu for the fix. + # data.table [v1.15.0](https://github.com/Rdatatable/data.table/milestone/29) (30 Jan 2024) ## BREAKING CHANGE From 9969646a90177059b3dbbd51dbb95d5ceeb62c01 Mon Sep 17 00:00:00 2001 From: joshhwuu Date: Fri, 12 Apr 2024 19:00:15 -0700 Subject: [PATCH 7/8] added tests --- inst/tests/tests.Rraw | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 4cdaaaa0f..1f8eb8498 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -18455,9 +18455,19 @@ test(2256.6, fread('a;b\n1,14;5', verbose=TRUE), data.table(a=1.14, b=5L), outpu DT = data.table(id = c(1, 1, 2, 2), a = 1:4, b = 5:8) test(2257, DT[ , c("c", "a") := .(a + 1, NULL), by=id], error="it's not possible to delete parts of a column") +# testing printing data.tables with na.print, #3152 DT = data.table(x=c(NA, "a", "b")) test(2258.1, capture.output(print(DT, na.print=".")), c(" x", "1: .", "2: a", "3: b")) test(2258.2, capture.output(print(DT, na.print="_")), c(" x", "1: _", "2: a", "3: b")) test(2258.3, capture.output(print(DT, na.print="NA")), c(" x", "1: NA", "2: a", "3: b")) test(2258.4, capture.output(print(DT, na.print=TRUE)), error="invalid 'na.print' specification") -test(2258.5, capture.output(print(DT, na.print=".", quote=TRUE)), c(' "x"', "1: .", '2: "a"', '3: "b"')) \ No newline at end of file +test(2258.5, capture.output(print(DT, na.print=".", quote=TRUE)), c(' "x"', "1: .", '2: "a"', '3: "b"')) +test(2258.6, capture.output(print(DT, na.print=".", right=TRUE)), c(" x", "1: .", "2: a", "3: b")) +# tests for other call sites +# col.names="none" +test(2258.7, capture.output(print(DT, na.print=".", col.names="none")), c("1: .", "2: a", "3: b")) +# printdots=TRUE, col.names="none" +DT = data.table(x = c(NA, "e", "b", "j", "w", NA)) +test(2258.8, capture.output(print(DT, na.print=".", topn=2, col.names="none")), c(" 1: .", " 2: e", "--- ", " 5: w", " 6: .")) +# printdots=TRUE, col.names!="none" +test(2258.9, capture.output(print(DT, na.print=".", topn=2)), c(" x", " 1: .", " 2: e", "--- ", " 5: w", " 6: .")) From e0c2c12dabbc3d41eff88711cf7d96dc71bcbba5 Mon Sep 17 00:00:00 2001 From: joshhwuu Date: Fri, 12 Apr 2024 21:09:44 -0700 Subject: [PATCH 8/8] review suggestions --- inst/tests/tests.Rraw | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 1f8eb8498..d2f1daab6 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -18466,8 +18466,8 @@ test(2258.6, capture.output(print(DT, na.print=".", right=TRUE)), c(" x", "1: # tests for other call sites # col.names="none" test(2258.7, capture.output(print(DT, na.print=".", col.names="none")), c("1: .", "2: a", "3: b")) -# printdots=TRUE, col.names="none" +# table requires splitting, col.names="none" DT = data.table(x = c(NA, "e", "b", "j", "w", NA)) test(2258.8, capture.output(print(DT, na.print=".", topn=2, col.names="none")), c(" 1: .", " 2: e", "--- ", " 5: w", " 6: .")) -# printdots=TRUE, col.names!="none" +# table requires splitting, col.names!="none" test(2258.9, capture.output(print(DT, na.print=".", topn=2)), c(" x", " 1: .", " 2: e", "--- ", " 5: w", " 6: ."))