Skip to content

Commit 0bc19be

Browse files
Simplify 'class' handling in print() by asserting TRUE/FALSE up-front (#6937)
* Simplify 'class' handling by asserting TRUE/FALSE up-front * drop isTRUE() elsewhere
1 parent 1f448e4 commit 0bc19be

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

R/print.data.table.R

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ print.data.table = function(x, topn=getOption("datatable.print.topn"),
1919
stopf("Valid options for col.names are 'auto', 'top', and 'none'")
2020
if (length(trunc.cols) != 1L || !is.logical(trunc.cols) || is.na(trunc.cols))
2121
stopf("Valid options for trunc.cols are TRUE and FALSE")
22+
stopifnot(isTRUEorFALSE(class))
2223
if (col.names == "none" && class)
2324
warningf("Column classes will be suppressed when col.names is 'none'")
2425
if (!shouldPrint(x)) {
@@ -52,11 +53,11 @@ print.data.table = function(x, topn=getOption("datatable.print.topn"),
5253
))
5354
}
5455
if (any(dim(x)==0L)) {
55-
class = if (is.data.table(x)) "table" else "frame" # a data.frame could be passed to print.data.table() directly, #3363
56+
x_class = if (is.data.table(x)) "data.table" else "data.frame" # a data.frame could be passed to print.data.table() directly, #3363
5657
if (all(dim(x)==0L)) {
57-
catf("Null data.%s (0 rows and 0 cols)\n", class) # See FAQ 2.5 and NEWS item in v1.8.9
58+
catf("Null %s (0 rows and 0 cols)\n", x_class) # See FAQ 2.5 and NEWS item in v1.8.9
5859
} else {
59-
catf("Empty data.%s (%d rows and %d cols)", class, NROW(x), NCOL(x))
60+
catf("Empty %s (%d rows and %d cols)", x_class, NROW(x), NCOL(x))
6061
if (length(x)>0L) cat(": ",paste(head(names(x),6L),collapse=","),if(length(x)>6L)"...",sep="") # notranslate
6162
cat("\n") # notranslate
6263
}
@@ -94,7 +95,7 @@ print.data.table = function(x, topn=getOption("datatable.print.topn"),
9495
if (is.null(names(x)) || !any(nzchar(names(x), keepNA=TRUE)))
9596
# fixes bug #97 and #545
9697
colnames(toprint)=rep("", ncol(toprint))
97-
if (isTRUE(class) && col.names != "none") {
98+
if (class && col.names != "none") {
9899
#Matching table for most common types & their abbreviations
99100
class_abb = c(list = "<list>", integer = "<int>", numeric = "<num>",
100101
character = "<char>", Date = "<Date>", complex = "<cplx>",
@@ -105,8 +106,9 @@ print.data.table = function(x, topn=getOption("datatable.print.topn"),
105106
if ( length(idx <- which(is.na(abbs))) ) abbs[idx] = paste0("<", classes[idx], ">")
106107
toprint = rbind(abbs, toprint)
107108
rownames(toprint)[1L] = ""
109+
} else {
110+
abbs = ""
108111
}
109-
if (isFALSE(class) || (isTRUE(class) && col.names == "none")) abbs = ""
110112
if (quote) colnames(toprint) <- paste0('"', old <- colnames(toprint), '"')
111113
if (isTRUE(trunc.cols)) {
112114
# allow truncation of columns to print only what will fit in console PR #4074
@@ -131,9 +133,9 @@ print.data.table = function(x, topn=getOption("datatable.print.topn"),
131133
}
132134
if (printdots) {
133135
if (isFALSE(row.names)) {
134-
toprint = rbind(head(toprint, topn + isTRUE(class)), "---", tail(toprint, topn)) # 4083
136+
toprint = rbind(head(toprint, topn + class), "---", tail(toprint, topn)) # 4083
135137
} else {
136-
toprint = rbind(head(toprint, topn + isTRUE(class)), "---"="", tail(toprint, topn))
138+
toprint = rbind(head(toprint, topn + class), "---"="", tail(toprint, topn))
137139
}
138140
rownames(toprint) = format(rownames(toprint), justify="right")
139141
print_default(toprint)
@@ -145,7 +147,7 @@ print.data.table = function(x, topn=getOption("datatable.print.topn"),
145147
toprint = rbind(
146148
toprint,
147149
matrix(if (quote) old else colnames(toprint), nrow=1L), # see #97
148-
if (isTRUE(class)) matrix(if (trunc.cols) abbs[cols_to_print] else abbs, nrow=1L) # #6902
150+
if (class) matrix(if (trunc.cols) abbs[cols_to_print] else abbs, nrow=1L) # #6902
149151
)
150152
print_default(toprint)
151153
invisible(x)

0 commit comments

Comments
 (0)