Skip to content

Commit

Permalink
Fix char.trunc with NA (#6442)
Browse files Browse the repository at this point in the history
* omit NA in indices

* revisit

* better test

* change order

* NEWS

---------

Co-authored-by: Michael Chirico <[email protected]>
  • Loading branch information
joshhwuu and MichaelChirico committed Aug 31, 2024
1 parent 7c2ced2 commit 4ff8a16
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

# data.table [v1.16.1](https://github.com/Rdatatable/data.table/milestone/35)

## 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. Fixed a typo in the NEWS for the last release -- that's version 1.16.0, not 1.6.0; apologies. Thanks @r2evans for flagging, [#6443](https://github.com/Rdatatable/data.table/issues/6443).
Expand Down
2 changes: 1 addition & 1 deletion R/print.data.table.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -18507,6 +18507,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: <NA>")
})

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

0 comments on commit 4ff8a16

Please sign in to comment.