-
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
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) | ||
| t_names = char.trunc(head(names(x), 6L), trunc.char = e_trunc) | ||
| 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,12 @@ 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 | ||
| width_limit = max(0L, getOption("width") - rn_w - 3L) | ||
|
Comment on lines
+96
to
+97
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. same here please explain 0L 3L 2L? |
||
| trunc.char = getOption("datatable.prettyprint.char") %||% 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 = width_limit) | ||
|
|
||
| # 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 +126,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 = width_limit) | ||
| return(invisible(x)) | ||
| } | ||
| # When nrow(toprint) = 1, attributes get lost in the subset, | ||
|
|
@@ -134,7 +138,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 = width_limit) | ||
| } | ||
| if (printdots) { | ||
| if (isFALSE(row.names)) { | ||
|
|
@@ -253,7 +257,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 +295,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 |
|---|---|---|
|
|
@@ -21683,3 +21683,12 @@ max_ppsize = local({ | |
| x = as.data.table(as.list(1:max_ppsize)) | ||
| test(2376, rbindlist(list(x)), x) | ||
| rm(x, max_ppsize) | ||
|
|
||
| #7797 long column names go past width | ||
| test(2377.1, print(data.table(abcdefghijklmnopqrstuvwxyz=1)), output="abcdefghijklmn...", options=list(width=20)) | ||
| test(2377.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. please add a test with more than one column.
Contributor
Author
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. i added two tests , are they satisfying what you were excpecting.
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. this is better but the test output has regex so difficult for me to see what it actually prints as, can you please add the output of these tests in a PR comment? |
||
| test(2377.3, print(data.table(abcdefghijklmnopqrst=1)), output="abcdefghijklmnopqrst", options=list(width=30)) | ||
| test(2377.4, print(data.table(abcdefghijklmnopqrstuvwxyz=1, zyxwvutsrqponmlkjihgfedcba=2)), output="zyxwvutsrqponm...", options=list(width=40, datatable.print.trunc.cols=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. it looks like we're only asserting the footer output here? can we make the assertions stronger? |
||
| test(2377.5, 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, datatable.prettyprint.char=Inf)) | ||
| test(2377.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(2377.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.
can these 5L and 6L get put in variables with informative names so we can understand where they come from? or please at least add a comment to explain?
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.
just wanted to ask on thing that should `datatable.prettyprint.char = Inf`` also disable truncation of column names, or should column names always respect the available print width?
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.
this is what i got for the first twotests