-
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 8 commits
e7e92b1
021d6e2
864954e
1850b39
78621eb
2aec2a5
3e58a32
43dddf2
d638819
aa54da6
d4d80f1
0c69365
6757317
e697d45
61368bf
d21225b
4bc39fc
e908057
beb43fa
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, ...) { | ||||||
|
|
@@ -52,6 +53,9 @@ print.data.table = function(x, topn=getOption("datatable.print.topn"), | |||||
| paste0("<", ixs, ">", collapse = ", ") | ||||||
| )) | ||||||
| } | ||||||
| if (show.ncols && !isTRUE(trunc.cols) && !any(dim(x)==0L)) { | ||||||
| catf("Number of columns: %d\n", ncol(x)) | ||||||
|
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 use a helper here, similar to |
||||||
| } | ||||||
| if (any(dim(x)==0L)) { | ||||||
| x_class = if (is.data.table(x)) "data.table" else "data.frame" # a data.frame could be passed to print.data.table() directly, #3363 | ||||||
| if (all(dim(x)==0L)) { | ||||||
|
|
@@ -116,14 +120,26 @@ 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 |
||||||
| n_not_printed = length(not_printed) | ||||||
|
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 seems like a near copy of |
||||||
| if (n_not_printed > 0L) { | ||||||
| if (class && col.names != "none") classes = paste0(" ", tail(abbs, n_not_printed)) else classes = "" | ||||||
| catf("Number of columns: %d, of which %d %s not shown: %s\n", | ||||||
| ncol(x), n_not_printed, ngettext(n_not_printed, "is", "are"), | ||||||
| brackify(paste0(not_printed, classes))) | ||||||
| trunc.cols = 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.
|
||||||
| } else { | ||||||
| catf("Number of columns: %d\n", ncol(x)) | ||||||
| } | ||||||
|
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
wrong indentation |
||||||
| } | ||||||
| if (!any(cols_to_print)) { | ||||||
| trunc_cols_message(not_printed, abbs, class, col.names) | ||||||
| if (!show.ncols) trunc_cols_message(not_printed, abbs, class, col.names) | ||||||
| return(invisible(x)) | ||||||
| } | ||||||
| # When nrow(toprint) = 1, attributes get lost in the subset, | ||||||
| # function below adds those back when necessary | ||||||
| toprint = toprint_subset(toprint, cols_to_print) | ||||||
| trunc.cols = length(not_printed) > 0L | ||||||
| trunc.cols = !show.ncols && length(not_printed) > 0L | ||||||
| } | ||||||
| print_default = function(x) { | ||||||
| if (col.names != "none") cut_colnames = identity | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21660,3 +21660,11 @@ test(2374.08, key(DT[, .(a, a)]), NULL) | |
| test(2374.09, key(subset(DT, select=c(a, a))), NULL) | ||
| DT = data.table(a=1:2, a.1=3:4, val=10:11) | ||
| test(2374.10, key(DT[, .(a.1, sum(val)), keyby=.(a, a)]), NULL) | ||
|
|
||
| #6663 Option to print the number of columns | ||
| test(2375.1, {options(datatable.show.ncols=TRUE); DT=as.data.table(iris); capture.output(print(DT))[1L]}, "Number of columns: 5") | ||
|
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. We have an options parameter in Please only use bracket blocks Also you can set multiple options at once with |
||
| test(2375.2, {options(datatable.show.ncols=TRUE); DT=as.data.table(iris); setkey(DT, Sepal.Length); any(grepl("^Number of columns: 5$", capture.output(print(DT))))}, TRUE) | ||
| test(2375.3, {options(width=20); options(datatable.show.ncols=TRUE); DT=data.table(a=1:3,b=1:3,c=1:3,d=1:3,e=1:3); any(grepl("^5 variables not shown", capture.output(print(DT, trunc.cols=TRUE))))}, FALSE) | ||
| test(2375.4, {options(width=10); options(datatable.show.ncols=TRUE); DT=as.data.table(iris); capture.output(print(DT, trunc.cols=TRUE))[1L]}, "Number of columns: 5, of which 5 are not shown: [Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, Species]") | ||
| test(2375.5, {options(datatable.show.ncols=FALSE); DT=as.data.table(iris); any(grepl("^Number of columns:", capture.output(print(DT))))}, FALSE) | ||
| test(2375.6, {options(width=10); options(datatable.show.ncols=TRUE); DT=as.data.table(iris); capture.output(print(DT, trunc.cols=TRUE, class=TRUE))[1L]}, "Number of columns: 5, of which 5 are not shown: [Sepal.Length <num>, Sepal.Width <num>, Petal.Length <num>, Petal.Width <num>, Species <fctr>]") | ||
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 we move this below the
any(dim(x)==0L)branch which comes next and drop the!any(dim(x)==0L)guard?