Skip to content

Commit beb43fa

Browse files
committed
updated logic and added test
1 parent e908057 commit beb43fa

2 files changed

Lines changed: 10 additions & 8 deletions

File tree

R/print.data.table.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ print.data.table = function(x, topn=getOption("datatable.print.topn"),
4646
topnmiss = missing(topn)
4747
topn = max(as.integer(topn),1L)
4848
show_trunc_message = isTRUE(trunc.cols)
49+
n_col = ncol(x)
4950
if (print.keys) {
5051
if (!is.null(ky <- key(x)))
5152
catf("Key: <%s>\n", toString(ky))
@@ -89,7 +90,7 @@ print.data.table = function(x, topn=getOption("datatable.print.topn"),
8990
if (show.indices) toprint = cbind(toprint, index_dt)
9091
}
9192
if (show.ncols && !isTRUE(trunc.cols)) {
92-
trunc_cols_message(character(0), NULL, FALSE, "none", ncol=ncol(toprint))
93+
trunc_cols_message(character(0), NULL, FALSE, "none", ncol=n_col)
9394
}
9495
require_bit64_if_needed(x)
9596
classes = classes1(toprint)
@@ -127,7 +128,7 @@ print.data.table = function(x, topn=getOption("datatable.print.topn"),
127128
cols_to_print = widths < cons_width
128129
not_printed = colnames(toprint)[!cols_to_print]
129130
if (show.ncols) {
130-
trunc_cols_message(not_printed, abbs, class, col.names, ncol=ncol(toprint))
131+
trunc_cols_message(not_printed, abbs, class, col.names, ncol=n_col)
131132
show_trunc_message = FALSE
132133
}
133134

inst/tests/tests.Rraw

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21777,11 +21777,12 @@ test(2377.91, truelength(dt$a), 0L)
2177721777
test(2377.92, {setallocrow(dt); truelength(dt$a)}, 3L)
2177821778

2177921779
#6663 Option to print the number of columns
21780-
test(2378.1, capture.output(print(as.data.table(iris)))[1L], "Number of columns: 5", options=list(datatable.show.ncols=TRUE))
21781-
test(2378.2, any(grepl("^Number of columns: 5$", capture.output(print(setkey(as.data.table(iris), Sepal.Length))))), TRUE, options=list(datatable.show.ncols=TRUE))
21782-
test(2378.3, any(grepl("^[0-9]+ variables? not shown", capture.output(print(data.table(a=1:3, b=1:3, c=1:3, d=1:3, e=1:3), trunc.cols=TRUE)))), FALSE, options=list(width=20, datatable.show.ncols=TRUE))
21783-
test(2378.4, capture.output(print(as.data.table(iris), trunc.cols=TRUE))[1L], "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))
21784-
test(2378.5, any(grepl("^Number of columns:", capture.output(print(as.data.table(iris))))), FALSE, options=list(datatable.show.ncols=FALSE))
21785-
test(2378.6, capture.output(print(as.data.table(iris), 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>]", options=list(width=10, datatable.show.ncols=TRUE))
21780+
test(2378.1, print(as.data.table(iris)), output="Number of columns: 5", options=list(datatable.show.ncols=TRUE))
21781+
test(2378.2, print(setkey(as.data.table(iris), Sepal.Length)), output="Number of columns: 5", options=list(datatable.show.ncols=TRUE))
21782+
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))
21783+
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))
21784+
test(2378.5, any(grepl("Number of columns:", capture.output(print(as.data.table(iris))))), FALSE, options=list(datatable.show.ncols=FALSE))
21785+
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))
2178621786
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))
2178721787
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))
21788+
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")

0 commit comments

Comments
 (0)