-
Notifications
You must be signed in to change notification settings - Fork 1k
Fix truncation of long column names in print.data.table() #7802
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
base: master
Are you sure you want to change the base?
Changes from all commits
249fd7a
4200f70
ab897c5
30a00ef
c4b3481
52990f1
9873073
3699adc
13fcfc9
1b5fb87
9be87b8
1d158e7
3ddef6e
5d62f50
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,7 +58,12 @@ print.data.table = function(x, topn=getOption("datatable.print.topn"), | |
| catf("Null %s (0 rows and 0 cols)\n", x_class) # See FAQ 2.5 and NEWS item in v1.8.9 | ||
| } else { | ||
| catf("Empty %s (%d rows and %d cols)", x_class, NROW(x), NCOL(x)) | ||
| if (length(x)>0L) cat(": ",paste(head(names(x),6L),collapse=","),if(length(x)>6L)"...",sep="") # notranslate | ||
| if (length(x)>0L) { | ||
| # Minimal truncation for empty table summary line | ||
| e_trunc = getOption("datatable.prettyprint.char") %||% (getOption("width") - 5L)# 5L accounts for "Empty data.table..." prefix; | ||
| t_names = char.trunc(head(names(x), 6L), trunc.char = e_trunc)#6L is the max number of names shown in summary | ||
| cat(": ", paste(t_names, collapse=","), if(length(x)>6L)"...", sep="") # notranslate | ||
| } | ||
| cat("\n") # notranslate | ||
| } | ||
| return(invisible(x)) | ||
|
|
@@ -74,7 +79,6 @@ print.data.table = function(x, topn=getOption("datatable.print.topn"), | |
| } | ||
| n_x = nrow(x) | ||
| if ((topn*2L+1L)<n_x && (n_x>nrows || !topnmiss)) { | ||
| toprint = rbindlist(list(head(x, topn), tail(x, topn)), use.names=FALSE) # no need to match names because head and tail of same x, and #3306 | ||
| rn = c(seq_len(topn), seq.int(to=n_x, length.out=topn)) | ||
| printdots = TRUE | ||
| idx = c(seq_len(topn), seq(to=nrow(x), length.out=topn)) | ||
|
|
@@ -88,12 +92,14 @@ print.data.table = function(x, topn=getOption("datatable.print.topn"), | |
| } | ||
| require_bit64_if_needed(x) | ||
| classes = classes1(toprint) | ||
| trunc.char = getOption("datatable.prettyprint.char") | ||
| if (is.null(trunc.char)) { | ||
| rn_w = if (isTRUE(row.names)) nchar(as.character(max(rn))) + 2L else 0L | ||
| trunc.char = max(0L, getOption("width") - rn_w - 3L) | ||
| } | ||
|
|
||
| rn_w = if (isTRUE(row.names)) nchar(as.character(max(rn))) + 2L else 0L# 2L accounts for the ": " added to row names (e.g., "10: "); 0L used when row.names=FALSE | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please leave spaces between code and comments. |
||
| width_limit = max(0L, getOption("width") - rn_w - 3L)# 3L accounts for separators and internal padding between columns | ||
| trunc.char = getOption("datatable.prettyprint.char") %||% width_limit | ||
| h_opt = getOption("datatable.prettyprint.char") | ||
| h_trunc = if (!is.null(h_opt) && is.infinite(h_opt)) Inf else width_limit | ||
| toprint=format.data.table(toprint, na.encode=FALSE, timezone = timezone, trunc.char = trunc.char, ...) # na.encode=FALSE so that NA in character cols print as <NA> | ||
| if (col.names != "none") colnames(toprint) = char.trunc(colnames(toprint), trunc.char = h_trunc) | ||
|
|
||
| # FR #353 - add row.names = logical argument to print.data.table | ||
| if (isTRUE(row.names)) rownames(toprint)=paste0(format(rn,right=TRUE,scientific=FALSE),":") else rownames(toprint)=rep.int("", nrow(toprint)) | ||
|
|
@@ -122,7 +128,7 @@ print.data.table = function(x, topn=getOption("datatable.print.topn"), | |
| cols_to_print = widths < cons_width | ||
| not_printed = colnames(toprint)[!cols_to_print] | ||
| if (!any(cols_to_print)) { | ||
| trunc_cols_message(not_printed, abbs, class, col.names) | ||
| trunc_cols_message(not_printed, abbs, class, col.names, trunc.char = h_trunc) | ||
| return(invisible(x)) | ||
| } | ||
| # When nrow(toprint) = 1, attributes get lost in the subset, | ||
|
|
@@ -134,7 +140,7 @@ print.data.table = function(x, topn=getOption("datatable.print.topn"), | |
| if (col.names != "none") cut_colnames = identity | ||
| cut_colnames(print(x, right=TRUE, quote=quote, na.print=na.print)) | ||
| # prints names of variables not shown in the print | ||
| if (trunc.cols) trunc_cols_message(not_printed, abbs, class, col.names) | ||
| if (trunc.cols) trunc_cols_message(not_printed, abbs, class, col.names, trunc.char = h_trunc) | ||
| } | ||
| if (printdots) { | ||
| if (isFALSE(row.names)) { | ||
|
|
@@ -253,7 +259,7 @@ format_list_item.data.frame = function(x, ...) { | |
| # Current implementation may have issues when dealing with strings that have combinations of full-width and half-width characters, | ||
| # if this becomes a problem in the future, we could consider string traversal instead. | ||
| char.trunc = function(x, trunc.char = getOption("datatable.prettyprint.char")) { | ||
| if (is.null(trunc.char)) return(x) | ||
| if (is.null(trunc.char) || is.infinite(trunc.char[1L])) return(x) | ||
| trunc.char = max(0L, suppressWarnings(as.integer(trunc.char[1L])), na.rm=TRUE) | ||
| if (!is.character(x) || trunc.char <= 0L) return(x) | ||
| nchar_width = nchar(x, 'width', allowNA = TRUE) | ||
|
|
@@ -291,12 +297,12 @@ toprint_subset = function(x, cols_to_print) { | |
| } | ||
| } | ||
| # message for when trunc.cols=TRUE and some columns are not printed | ||
| trunc_cols_message = function(not_printed, abbs, class, col.names){ | ||
| trunc_cols_message = function(not_printed, abbs, class, col.names, trunc.char = getOption("datatable.prettyprint.char")){ | ||
| n = length(not_printed) | ||
| if (class && col.names != "none") classes = paste0(" ", tail(abbs, n)) else classes = "" | ||
| catf( | ||
| ngettext(n, "%d variable not shown: %s\n", "%d variables not shown: %s\n"), | ||
| n, brackify(paste0(not_printed, classes)), | ||
| n, brackify(paste0(char.trunc(not_printed, trunc.char = trunc.char), classes)), | ||
| domain=NA | ||
| ) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21775,3 +21775,12 @@ test(2377.44, copy(dt)[!(a < 5 & b != "d"), .ROW := NULL], dt[1:3]) | |
| dt = data.table(a=1:3) | ||
| test(2377.91, truelength(dt$a), 0L) | ||
| test(2377.92, {setallocrow(dt); truelength(dt$a)}, 3L) | ||
|
|
||
| #7797 long column names go past width | ||
| test(2378.1, print(data.table(abcdefghijklmnopqrstuvwxyz=1)), output="abcdefghijklmn...", options=list(width=20)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please remove the bloat |
||
| test(2378.2, print(data.table(abcdefghijklmnopqrstuvwxyz=1)), output="1 variable not shown: \\[abcdefghijklmn... <num>\\]", options=list(width=20, datatable.print.trunc.cols=TRUE, datatable.print.class=TRUE)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this test really respecting width of 20? |
||
| test(2378.3, print(data.table(abcdefghijklmnopqrst=1)), output="abcdefghijklmnopqrst", options=list(width=30)) | ||
| test(2378.4, print(data.table(abcdefghijklmnopqrstuvwxyz=1, zyxwvutsrqponmlkjihgfedcba=2)), output="abcdefghijklmnopqrstuvwxyz.*1 variable not shown: \\[zyxwvutsrqponmlkjihgfedcba <num>\\]", options=list(width=35, datatable.print.trunc.cols=TRUE, datatable.print.class=TRUE)) | ||
| test(2378.5, print(data.table(abcdefghijklmnopqrstuvwxyz=1)), output="1 variable not shown: \\[abcdefghijklmnopqrstuvwxyz <num>\\]", options=list(width=20, datatable.print.trunc.cols=TRUE, datatable.print.class=TRUE, datatable.prettyprint.char=Inf)) | ||
| test(2378.6, print(data.table(very_long_name_a=1, s_b=2, very_long_name_c=3)), output="very_long_name_a[ ]+s_b.*1 variable not shown: \\[very_long_name_c <num>\\]", options=list(width=35, datatable.print.trunc.cols=TRUE, datatable.print.class=TRUE)) | ||
| test(2378.7, print(data.table(extremely_long_a=1, s_b=2, extremely_long_c=3, s_d=4)), output="extremely_long_a[ ]+s_b.*2 variables not shown: \\[extremely_long_c <num>, s_d <num>\\]", options=list(width=30, datatable.print.trunc.cols=TRUE, datatable.print.class=TRUE)) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IINM
%||%was introduced in R 4.4.0? But we support R 3.5.0