Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "na.print" as a new argument to "print.data.table" #6087

Merged
merged 9 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions R/print.data.table.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
MichaelChirico marked this conversation as resolved.
Show resolved Hide resolved
} 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
Expand All @@ -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
Expand Down
17 changes: 17 additions & 0 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -18454,3 +18454,20 @@ 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")

# testing printing data.tables with na.print, #3152
DT = data.table(x=c(NA, "a", "b"))
MichaelChirico marked this conversation as resolved.
Show resolved Hide resolved
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")
joshhwuu marked this conversation as resolved.
Show resolved Hide resolved
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"
joshhwuu marked this conversation as resolved.
Show resolved Hide resolved
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"
joshhwuu marked this conversation as resolved.
Show resolved Hide resolved
test(2258.9, capture.output(print(DT, na.print=".", topn=2)), c(" x", " 1: .", " 2: e", "--- ", " 5: w", " 6: ."))
2 changes: 2 additions & 0 deletions man/print.data.table.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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, as in \code{print.default}. }
\item{\dots}{ Other arguments ultimately passed to \code{format}. }
}
\value{
Expand Down
Loading