From 497b22a3b5ebfcdc90ece3421730cbe2be34dc23 Mon Sep 17 00:00:00 2001 From: Joshua Wu Date: Fri, 30 Aug 2024 18:25:04 -0700 Subject: [PATCH] Fix char.trunc with NA (#6442) * omit NA in indices * revisit * better test * change order * NEWS --------- Co-authored-by: Michael Chirico --- NEWS.md | 4 ++++ R/print.data.table.R | 2 +- inst/tests/tests.Rraw | 2 ++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 8b15449e8..7bc37a974 100644 --- a/NEWS.md +++ b/NEWS.md @@ -6,6 +6,10 @@ 1. In `DT[, variable := value]`, when value is class `POSIXlt`, we automatically coerce it to class `POSIXct` instead, [#1724](https://github.com/Rdatatable/data.table/issues/1724). Thanks to @linzhp for the report, and Benjamin Schwendinger for the fix. +## BUG FIXES + +1. Using `print.data.table()` with character truncation using `datatable.prettyprint.char` no longer errors with `NA` entries, [#6441](https://github.com/Rdatatable/data.table/issues/6441). Thanks to @r2evans for the bug report, and @joshhwuu for the 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. diff --git a/R/print.data.table.R b/R/print.data.table.R index 528138279..280582c5e 100644 --- a/R/print.data.table.R +++ b/R/print.data.table.R @@ -247,7 +247,7 @@ char.trunc = function(x, trunc.char = getOption("datatable.prettyprint.char")) { nchar_width = nchar(x, 'width') # Check whether string is full-width or half-width, #5096 nchar_chars = nchar(x, 'char') is_full_width = nchar_width > nchar_chars - idx = pmin(nchar_width, nchar_chars) > trunc.char + idx = !is.na(x) & pmin(nchar_width, nchar_chars) > trunc.char if (!any(idx)) return(x) # strtrim() errors for width=integer() on R 3.3.0 x[idx] = paste0(strtrim(x[idx], trunc.char * fifelse(is_full_width[idx], 2L, 1L)), "...") x diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 55712e009..bc06b4517 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -18508,6 +18508,8 @@ local({ c(paste0(ja_ko, " ", paste0(ja_n, dots), " ", paste0(accented_a, dots)), paste0(c(ja_ko, ja_n, accented_a), dots, collapse=" "), paste0(c(ja_ko, ja_n, accented_a), dots, collapse=" "))) + # test for data.table with NA, #6441 + test(2253.20, options=list(datatable.prettyprint.char = 1L), data.table(a = c("abc", NA)), output=" a\n1: a...\n2: ") }) # allow 1-D matrix in j for consistency, #783