Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

6. `yearqtr()` and `yearmon()` now gain an optional format specifier [#7694](https://github.com/Rdatatable/data.table/issues/7694). 'numeric' is the default, which preserves the original behavior, but 'character' formats `yearqtr()` as YYYYQ# (e.g. 2025Q2) and `yearmon()` as YYYYM## (e.g. 2025M02, 2025M10). Thanks to @jan-swissre for the report and @LunaticSage218 for the implementation.

7. `print.data.table()` gains a `show.ncols` argument and `datatable.show.ncols` option to print the number of columns (labeled as `ncol:`) in the header, [#6663](https://github.com/Rdatatable/data.table/issues/6663). When `trunc.cols=TRUE`, this information is merged into the truncation message at the top, and the redundant footer message is suppressed. Thanks to @KyleHaynes for the suggestion and @venom1204 for the implementation.

### BUG FIXES

1. `fread()` with `skip=0` and `(header=TRUE|FALSE)` no longer skips the first row when it has fewer fields than subsequent rows, [#7463](https://github.com/Rdatatable/data.table/issues/7463). Thanks @emayerhofer for the report and @ben-schwen for the fix.
Expand Down
1 change: 1 addition & 0 deletions R/onLoad.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
datatable.print.keys=TRUE, # for print.data.table
datatable.print.trunc.cols=FALSE, # for print.data.table
datatable.show.indices=FALSE, # for print.data.table
datatable.show.ncols=FALSE, # for print.data.table
datatable.allow.cartesian=FALSE, # datatable.<argument name>
datatable.join.many=TRUE, # mergelist, [.data.table #4383 #914
datatable.dfdispatchwarn=TRUE, # not a function argument
Expand Down
20 changes: 18 additions & 2 deletions R/print.data.table.R
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...) {
Expand Down Expand Up @@ -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))
}
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)) {
Expand Down Expand Up @@ -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) {
n_not_printed = length(not_printed)
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
} else {
catf("Number of columns: %d\n", ncol(x))
}
}
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
Expand Down
8 changes: 8 additions & 0 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -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")
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>]")
1 change: 1 addition & 0 deletions man/data.table-options.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
\item{\code{datatable.print.keys}}{A logical, default \code{FALSE}. If \code{TRUE}, the table's
keys are printed above the data.}
\item{\code{datatable.show.indices}}{A logical, default \code{TRUE}. A synonym for \code{datatable.print.keys} for historical reasons.}
\item{\code{datatable.show.ncols}}{A logical, default \code{FALSE}. If \code{TRUE}, the number of columns is printed in the header.}
\item{\code{datatable.print.trunc.cols}}{A logical, default \code{FALSE}. If \code{TRUE} and a
table has more columns than fit on the screen, it truncates the middle columns.}
\item{\code{datatable.prettyprint.char}}{An integer, default \code{100L}. The maximum number of
Expand Down
2 changes: 2 additions & 0 deletions man/print.data.table.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -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
quote=FALSE,
na.print=NULL,
timezone=FALSE, \dots)
Expand All @@ -43,6 +44,7 @@
\item{nrows}{ The number of rows which will be printed before truncation is enforced. }
\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}). }
Expand Down
Loading