@@ -44,6 +44,7 @@ print.data.table = function(x, topn=getOption("datatable.print.topn"),
4444 if (! is.numeric(topn )) topn = 5L
4545 topnmiss = missing(topn )
4646 topn = max(as.integer(topn ),1L )
47+ show_trunc_message = isTRUE(trunc.cols )
4748 if (print.keys ) {
4849 if (! is.null(ky <- key(x )))
4950 catf(" Key: <%s>\n " , toString(ky ))
@@ -54,7 +55,7 @@ print.data.table = function(x, topn=getOption("datatable.print.topn"),
5455 ))
5556 }
5657 if (show.ncols && ! isTRUE(trunc.cols ) && ! any(dim(x )== 0L )) {
57- catf( " Number of columns: %d \n " , ncol(x ))
58+ trunc_cols_message( character ( 0 ), NULL , FALSE , " none " , ncol = ncol(x ))
5859 }
5960 if (any(dim(x )== 0L )) {
6061 x_class = if (is.data.table(x )) " data.table" else " data.frame" # a data.frame could be passed to print.data.table() directly, #3363
@@ -126,31 +127,24 @@ print.data.table = function(x, topn=getOption("datatable.print.topn"),
126127 cols_to_print = widths < cons_width
127128 not_printed = colnames(toprint )[! cols_to_print ]
128129 if (show.ncols ) {
129- n_not_printed = length(not_printed )
130- if (n_not_printed > 0L ) {
131- if (class && col.names != " none" ) classes = paste0(" " , tail(abbs , n_not_printed )) else classes = " "
132- catf(" Number of columns: %d, of which %d %s not shown: %s\n " ,
133- ncol(x ), n_not_printed , ngettext(n_not_printed , " is" , " are" ),
134- brackify(paste0(not_printed , classes )))
135- trunc.cols = FALSE
136- } else {
137- catf(" Number of columns: %d\n " , ncol(x ))
130+ trunc_cols_message(not_printed , abbs , class , col.names , ncol = ncol(x ))
131+ show_trunc_message = FALSE
138132 }
139- }
133+
140134 if (! any(cols_to_print )) {
141- if (! show.ncols ) trunc_cols_message(not_printed , abbs , class , col.names )
135+ if (show_trunc_message ) trunc_cols_message(not_printed , abbs , class , col.names )
142136 return (invisible (x ))
143137 }
144138 # When nrow(toprint) = 1, attributes get lost in the subset,
145139 # function below adds those back when necessary
146140 toprint = toprint_subset(toprint , cols_to_print )
147- trunc.cols = ! show.ncols && length(not_printed ) > 0L
141+ trunc.cols = length(not_printed ) > 0L
148142 }
149143 print_default = function (x ) {
150144 if (col.names != " none" ) cut_colnames = identity
151145 cut_colnames(print(x , right = TRUE , quote = quote , na.print = na.print ))
152146 # prints names of variables not shown in the print
153- if (trunc.cols ) trunc_cols_message(not_printed , abbs , class , col.names )
147+ if (show_trunc_message ) trunc_cols_message(not_printed , abbs , class , col.names )
154148 }
155149 if (printdots ) {
156150 if (isFALSE(row.names )) {
@@ -307,14 +301,28 @@ toprint_subset = function(x, cols_to_print) {
307301 }
308302}
309303# message for when trunc.cols=TRUE and some columns are not printed
310- trunc_cols_message = function (not_printed , abbs , class , col.names ){
304+ trunc_cols_message = function (not_printed , abbs , class , col.names , ncol = NULL ){
311305 n = length(not_printed )
312- if (class && col.names != " none" ) classes = paste0(" " , tail(abbs , n )) else classes = " "
313- catf(
314- ngettext(n , " %d variable not shown: %s\n " , " %d variables not shown: %s\n " ),
315- n , brackify(paste0(not_printed , classes )),
316- domain = NA
317- )
306+ if (is.null(ncol )) {
307+ if (n == 0L ) return ()
308+ if (class && col.names != " none" ) classes = paste0(" " , tail(abbs , n )) else classes = " "
309+ catf(
310+ ngettext(n , " %d variable not shown: %s\n " , " %d variables not shown: %s\n " ),
311+ n , brackify(paste0(not_printed , classes )),
312+ domain = NA
313+ )
314+ } else {
315+ if (n > 0L ) {
316+ if (class && col.names != " none" ) classes = paste0(" " , tail(abbs , n )) else classes = " "
317+ catf(
318+ 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 " ),
319+ ncol , n , brackify(paste0(not_printed , classes )),
320+ domain = NA
321+ )
322+ } else {
323+ catf(" Number of columns: %d\n " , ncol )
324+ }
325+ }
318326}
319327
320328# Maybe add a method for repr::repr_text. See https://github.com/Rdatatable/data.table/issues/933#issuecomment-220237965
0 commit comments