-
Notifications
You must be signed in to change notification settings - Fork 1k
Adds optional column count display to print.data.table()
#7791
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 19 commits
e7e92b1
021d6e2
864954e
1850b39
78621eb
2aec2a5
3e58a32
43dddf2
d638819
aa54da6
d4d80f1
0c69365
6757317
e697d45
61368bf
d21225b
4bc39fc
e908057
beb43fa
3e38a5b
84c707b
a8d3284
fbe05f3
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 |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ print.data.table = function(x, topn=getOption("datatable.print.topn"), | |
| print.keys=getOption("datatable.print.keys"), | ||
| trunc.cols=getOption("datatable.print.trunc.cols"), | ||
| show.indices=getOption("datatable.show.indices"), | ||
| show.ncols=getOption("datatable.show.ncols", FALSE), | ||
| quote=FALSE, | ||
| na.print=NULL, | ||
| timezone=FALSE, ...) { | ||
|
|
@@ -20,6 +21,7 @@ print.data.table = function(x, topn=getOption("datatable.print.topn"), | |
| if (length(trunc.cols) != 1L || !is.logical(trunc.cols) || is.na(trunc.cols)) | ||
| stopf("Valid options for trunc.cols are TRUE and FALSE") | ||
| stopifnot(isTRUEorFALSE(class)) | ||
| stopifnot(isTRUEorFALSE(show.ncols)) | ||
| if (col.names == "none" && class) | ||
| warningf("Column classes will be suppressed when col.names is 'none'") | ||
| if (!shouldPrint(x)) { | ||
|
|
@@ -43,6 +45,8 @@ print.data.table = function(x, topn=getOption("datatable.print.topn"), | |
| if (!is.numeric(topn)) topn = 5L | ||
| topnmiss = missing(topn) | ||
| topn = max(as.integer(topn),1L) | ||
| show_trunc_message = isTRUE(trunc.cols) | ||
| n_col = ncol(x) | ||
| if (print.keys) { | ||
| if (!is.null(ky <- key(x))) | ||
| catf("Key: <%s>\n", toString(ky)) | ||
|
|
@@ -74,7 +78,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)) | ||
|
|
@@ -86,6 +89,9 @@ print.data.table = function(x, topn=getOption("datatable.print.topn"), | |
| printdots = FALSE | ||
| if (show.indices) toprint = cbind(toprint, index_dt) | ||
| } | ||
| if (show.ncols && !isTRUE(trunc.cols)) { | ||
| trunc_cols_message(character(0), NULL, FALSE, "none", ncol=n_col) | ||
| } | ||
| require_bit64_if_needed(x) | ||
| classes = classes1(toprint) | ||
| trunc.char = getOption("datatable.prettyprint.char") | ||
|
|
@@ -121,8 +127,13 @@ print.data.table = function(x, topn=getOption("datatable.print.topn"), | |
| cons_width = getOption("width") | ||
| cols_to_print = widths < cons_width | ||
| not_printed = colnames(toprint)[!cols_to_print] | ||
| if (show.ncols) { | ||
|
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. The column count seems to be wrong when using DT = data.table(a=1:3, b=1:3, c=1:3, d=1:3, e=1:3)
setindex(DT, a)
options(width=20, datatable.show.ncols=TRUE)
print(DT, trunc.cols=TRUE, show.indices=TRUE)
# Index: <a>
# Number of columns: 5, of which 4 are not shown: [c <int>, d <int>, e <int>, index:a <int>]
# a b
# <int> <int>
# 1: 1 1
# 2: 2 2
# 3: 3 3 |
||
| trunc_cols_message(not_printed, abbs, class, col.names, ncol=n_col) | ||
| show_trunc_message = FALSE | ||
| } | ||
|
|
||
| if (!any(cols_to_print)) { | ||
| trunc_cols_message(not_printed, abbs, class, col.names) | ||
| if (show_trunc_message) trunc_cols_message(not_printed, abbs, class, col.names) | ||
| return(invisible(x)) | ||
| } | ||
| # When nrow(toprint) = 1, attributes get lost in the subset, | ||
|
|
@@ -134,7 +145,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 (show_trunc_message) trunc_cols_message(not_printed, abbs, class, col.names) | ||
| } | ||
| if (printdots) { | ||
| if (isFALSE(row.names)) { | ||
|
|
@@ -291,14 +302,28 @@ 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, ncol=NULL){ | ||
|
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. If we have some decent default arg values for |
||
| 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)), | ||
| domain=NA | ||
| ) | ||
| if (is.null(ncol)) { | ||
|
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. maybe we make the branching here more clear with if (n == 0L) {
if (!is.null(ncol)) catf("Number of columns: %d\n", ncol)
return()
} |
||
| if (n == 0L) return() | ||
| 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)), | ||
| domain=NA | ||
| ) | ||
| } else { | ||
| if (n > 0L) { | ||
| if (class && col.names != "none") classes = paste0(" ", tail(abbs, n)) else classes = "" | ||
| catf( | ||
| ngettext(n, "Number of columns: %d, of which %d is not shown: %s\n", "Number of columns: %d, of which %d are not shown: %s\n"), | ||
| ncol, n, brackify(paste0(not_printed, classes)), | ||
| domain=NA | ||
| ) | ||
| } else { | ||
| catf("Number of columns: %d\n", ncol) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| # Maybe add a method for repr::repr_text. See https://github.com/Rdatatable/data.table/issues/933#issuecomment-220237965 | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -21775,3 +21775,14 @@ 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) | ||||||
|
|
||||||
| #6663 Option to print the number of columns | ||||||
| test(2378.1, print(as.data.table(iris)), output="Number of columns: 5", options=list(datatable.show.ncols=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.
Suggested change
|
||||||
| test(2378.2, print(setkey(as.data.table(iris), Sepal.Length)), output="Number of columns: 5", options=list(datatable.show.ncols=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.
Suggested change
I have made now two suggestions, but the same principle might apply to the later tests! |
||||||
| test(2378.3, { dt = data.table(a=1:3, b=1:3, c=1:3, d=1:3, e=1:3); any(grepl("^[0-9]+ variables? not shown", capture.output(print(dt, trunc.cols=TRUE)))) }, FALSE, options=list(width=20, datatable.show.ncols=TRUE)) | ||||||
| test(2378.4, print(as.data.table(iris), trunc.cols=TRUE), output="Number of columns: 5, of which 5 are not shown: [Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, Species]", options=list(width=10, datatable.show.ncols=TRUE)) | ||||||
| test(2378.5, any(grepl("Number of columns:", capture.output(print(as.data.table(iris))))), FALSE, options=list(datatable.show.ncols=FALSE)) | ||||||
| test(2378.6, print(as.data.table(iris), trunc.cols=TRUE, class=TRUE), output="Number of columns: 5, of which 5 are not shown: [Sepal.Length <num>, Sepal.Width <num>, Petal.Length <num>, Petal.Width <num>, Species <fctr>]", options=list(width=10, datatable.show.ncols=TRUE)) | ||||||
| test(2378.7, { dt = data.table(matrix(1:200, nrow=25)); out = capture.output(print(dt, trunc.cols=TRUE, class=TRUE)); grepl("<int>", out[length(out)]) }, TRUE, options=list(width=30, datatable.show.ncols=TRUE)) | ||||||
| test(2378.8, any(grepl("variable", capture.output(print(data.table(a=1), trunc.cols=TRUE)))), FALSE, options=list(width=80, datatable.show.ncols=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. Maybe we can add a test about an index like? do we also always need the test(2378.9, print(setindex(data.table(a=1:2, b=3:4), a), show.ncols=TRUE, show.indices=TRUE), output="Number of columns: 2") |
||||||
| test(2378.9, print(setindex(data.table(a=1:2, b=3:4), a), show.ncols=TRUE, show.indices=TRUE), output="Number of columns: 2") | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ | |
| print.keys=getOption("datatable.print.keys"), # default: TRUE | ||
| trunc.cols=getOption("datatable.print.trunc.cols"), # default: FALSE | ||
| show.indices=getOption("datatable.show.indices"), # default: FALSE | ||
| show.ncols=getOption("datatable.show.ncols", FALSE), # default: 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. why do we need to pass default when we set onLoad? |
||
| quote=FALSE, | ||
| na.print=NULL, | ||
| timezone=FALSE, \dots) | ||
|
|
@@ -44,6 +45,7 @@ | |
| \item{trunc.char}{The number of characters at which character columns and list-column summaries are truncated. If \code{NULL} (the default), it is dynamically calculated based on \code{getOption("width")}.} | ||
| \item{class}{ If \code{TRUE}, the resulting output will include above each column its storage class (or a self-evident abbreviation thereof). When combined with \code{col.names="auto"} and tables >20 rows, classes will also appear at the bottom.} | ||
| \item{row.names}{ If \code{TRUE}, row indices will be printed alongside \code{x}. } | ||
| \item{show.ncols}{ If \code{TRUE}, the number of columns is printed in the header. } | ||
| \item{col.names}{ One of three flavours for controlling the display of column names in output. \code{"auto"} includes column names above the data, as well as below the table if \code{nrow(x) > 20} (when \code{class=TRUE}, column classes will also appear at the bottom). \code{"top"} excludes this lower register when applicable, and \code{"none"} suppresses column names altogether (as well as column classes if \code{class = TRUE}. } | ||
| \item{print.keys}{ If \code{TRUE}, any \code{\link{key}} and/or \code{\link[=indices]{index}} currently assigned to \code{x} will be printed prior to the preview of the data. } | ||
| \item{trunc.cols}{ If \code{TRUE}, only the columns that can be printed in the console without wrapping the columns to new lines will be printed (similar to \code{tibbles}). } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
see comment below about
show.ncols=NA