Skip to content

Commit 497b22a

Browse files
Fix char.trunc with NA (#6442)
* omit NA in indices * revisit * better test * change order * NEWS --------- Co-authored-by: Michael Chirico <[email protected]>
1 parent 80c46a0 commit 497b22a

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
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.
88

9+
## BUG FIXES
10+
11+
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.
12+
913
## NOTES
1014

1115
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.

R/print.data.table.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ char.trunc = function(x, trunc.char = getOption("datatable.prettyprint.char")) {
247247
nchar_width = nchar(x, 'width') # Check whether string is full-width or half-width, #5096
248248
nchar_chars = nchar(x, 'char')
249249
is_full_width = nchar_width > nchar_chars
250-
idx = pmin(nchar_width, nchar_chars) > trunc.char
250+
idx = !is.na(x) & pmin(nchar_width, nchar_chars) > trunc.char
251251
if (!any(idx)) return(x) # strtrim() errors for width=integer() on R 3.3.0
252252
x[idx] = paste0(strtrim(x[idx], trunc.char * fifelse(is_full_width[idx], 2L, 1L)), "...")
253253
x

inst/tests/tests.Rraw

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18508,6 +18508,8 @@ local({
1850818508
c(paste0(ja_ko, " ", paste0(ja_n, dots), " ", paste0(accented_a, dots)),
1850918509
paste0(c(ja_ko, ja_n, accented_a), dots, collapse=" "),
1851018510
paste0(c(ja_ko, ja_n, accented_a), dots, collapse=" ")))
18511+
# test for data.table with NA, #6441
18512+
test(2253.20, options=list(datatable.prettyprint.char = 1L), data.table(a = c("abc", NA)), output=" a\n1: a...\n2: <NA>")
1851118513
})
1851218514

1851318515
# allow 1-D matrix in j for consistency, #783

0 commit comments

Comments
 (0)