Skip to content

Commit cca0282

Browse files
providing needed variables
1 parent 87ffd7f commit cca0282

File tree

1 file changed

+15
-25
lines changed

1 file changed

+15
-25
lines changed

R/print.data.table.R

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,23 @@ print.data.table = function(x, topn=getOption("datatable.print.topn"),
2222
if (col.names == "none" && class)
2323
warningf("Column classes will be suppressed when col.names is 'none'")
2424
if (!shouldPrint(x)) {
25-
# := in [.data.table sets .global$print=address(x) to suppress the next print i.e., like <- does. See FAQ 2.22 and README item in v1.9.5
25+
# := in [.data.table sets .global$print=address(x) to suppress the next print i.e., like <- does. See FAQ 2.22 and README item in v1.9.5
2626
# The issue is distinguishing "> DT" (after a previous := in a function) from "> DT[,foo:=1]". To print.data.table(), there
2727
# is no difference. Now from R 3.2.0 a side effect of the very welcome and requested change to avoid silent deep copy is that
2828
# there is now no longer a difference between > DT and > print(DT). So decided that DT[] is now needed to guarantee print; simpler.
2929
# This applies just at the prompt. Inside functions, print(DT) will of course print.
3030
# Other options investigated (could revisit): Cstack_info(), .Last.value gets set first before autoprint, history(), sys.status(),
3131
# topenv(), inspecting next statement in caller, using clock() at C level to timeout suppression after some number of cycles
3232
SYS = sys.calls()
33-
if (identical(SYS[[1L]][[1L]], print) || # this is what auto-print looks like, i.e. '> DT' and '> DT[, a:=b]' in the terminal; see #3029.
33+
if (identical(SYS[[1L]][[1L]], print) ||
3434
( length(SYS) >= 3L && is.symbol(thisSYS <- SYS[[length(SYS)-2L]][[1L]]) &&
35-
as.character(thisSYS) == 'source') ) { # suppress printing from source(echo = TRUE) calls, #2369
35+
as.character(thisSYS) == 'source') ) {
3636
return(invisible(x))
3737
}
3838
}
3939
if (!is.numeric(nrows)) nrows = 100L
4040
if (!is.infinite(nrows)) nrows = as.integer(nrows)
41-
if (nrows <= 0L) return(invisible(x)) # ability to turn off printing
41+
if (nrows <= 0L) return(invisible(x)) # ability to turn off printing
4242
if (!is.numeric(topn)) topn = 5L
4343
topnmiss = missing(topn)
4444
topn = max(as.integer(topn),1L)
@@ -57,29 +57,20 @@ print.data.table = function(x, topn=getOption("datatable.print.topn"),
5757
catf("Null data.%s (0 rows and 0 cols)\n", class) # See FAQ 2.5 and NEWS item in v1.8.9
5858
} else {
5959
catf("Empty data.%s (%d rows and %d cols)", class, NROW(x), NCOL(x))
60-
if (length(x)>0L) cat(": ",paste(head(names(x),6L),collapse=","),if(length(x)>6L)"...",sep="") # notranslate
61-
cat("\n") # notranslate
60+
if (length(x)>0L) cat(": ",paste(head(names(x),6L),collapse=","),if(length(x)>6L)"...",sep="") # notranslate
61+
cat("\n") # notranslate
6262
}
6363
return(invisible(x))
6464
}
6565
if (show.indices) {
66-
indices <- names(attr(x, "index", exact = TRUE))
67-
if (length(indices)) {
68-
cleaned_indices <- gsub("^__|_", ", ", indices)
69-
cleaned_indices <- sub(", $", "", cleaned_indices)
70-
71-
# Create header string (matches key display style)
72-
header <- paste0("Indices: ", paste(cleaned_indices, collapse = ", "))
73-
if (exists("header", envir = parent.frame(), inherits = FALSE)) {
74-
# Match data.table's existing header handling
75-
assign("header", c(get("header", envir = parent.frame()), header),
76-
envir = parent.frame())
77-
}
66+
if (is.null(indices(x))) {
67+
show.indices = FALSE
68+
} else {
69+
index_dt = as.data.table(attributes(attr(x, 'index')))
70+
print_names = paste0("index", if (ncol(index_dt) > 1L) seq_len(ncol(index_dt)) else "", ":", sub("^__", "", names(index_dt)))
71+
setnames(index_dt, print_names)
7872
}
79-
else {
80-
show.indices <- FALSE
81-
}
82-
}
73+
}
8374
n_x = nrow(x)
8475
if ((topn*2L+1L)<n_x && (n_x>nrows || !topnmiss)) {
8576
toprint = rbindlist(list(head(x, topn), tail(x, topn)), use.names=FALSE) # no need to match names because head and tail of same x, and #3306
@@ -100,7 +91,7 @@ print.data.table = function(x, topn=getOption("datatable.print.topn"),
10091
# FR #353 - add row.names = logical argument to print.data.table
10192
if (isTRUE(row.names)) rownames(toprint)=paste0(format(rn,right=TRUE,scientific=FALSE),":") else rownames(toprint)=rep.int("", nrow(toprint))
10293
if (is.null(names(x)) || !any(nzchar(names(x), keepNA=TRUE)))
103-
# fixes bug #97 and #545
94+
# fixes bug #97 and #545
10495
colnames(toprint)=rep("", ncol(toprint))
10596
if (isTRUE(class) && col.names != "none") {
10697
#Matching table for most common types & their abbreviations
@@ -145,12 +136,11 @@ print.data.table = function(x, topn=getOption("datatable.print.topn"),
145136
# When nrow(toprint) = 1, attributes get lost in the subset,
146137
# function below adds those back when necessary
147138
toprint = toprint_subset(toprint, cols_to_print)
148-
trunc.cols <- length(not_printed) > 0L
139+
trunc.cols = length(not_printed) > 0L
149140
}
150141
print_default = function(x) {
151142
if (col.names != "none") cut_colnames = identity
152143
cut_colnames(print(x, right=TRUE, quote=quote, na.print=na.print))
153-
# prints names of variables not shown in the print
154144
if (trunc.cols) trunc_cols_message(not_printed, abbs, class, col.names)
155145
}
156146
if (printdots) {

0 commit comments

Comments
 (0)