Skip to content

Commit a2c9184

Browse files
committed
Respect shouldPrint when auto-printing from knitr
Implementing a method for the knitr::knit_print generic makes it possible to customise the behaviour without looking up the call stack. The current solution only works on R >= 3.6.0 because that's where delayed S3 registration has been introduced.
1 parent 058dd4d commit a2c9184

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ if (getRversion() >= "4.0.0") {
104104
# version of R (and that is checked in .onLoad with error if not).
105105
export(.rbind.data.table) # only export in R<4.0.0 where it is still used; R-devel now detects it is missing doc, #5600
106106
}
107+
if (getRversion() >= "3.6.0") S3method(knitr::knit_print, data.table)
108+
# else manual delayed registration from the onLoad hook
107109
S3method(dim, data.table)
108110
S3method(dimnames, data.table)
109111
S3method("dimnames<-", data.table)

R/print.data.table.R

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,8 @@ print.data.table = function(x, topn=getOption("datatable.print.topn"),
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 (length(SYS) <= 2L || # "> DT" auto-print or "> print(DT)" explicit print (cannot distinguish from R 3.2.0 but that's ok)
34-
( length(SYS) >= 3L && is.symbol(thisSYS <- SYS[[length(SYS)-2L]][[1L]]) &&
35-
as.character(thisSYS) == 'source') || # suppress printing from source(echo = TRUE) calls, #2369
36-
( length(SYS) > 3L && is.symbol(thisSYS <- SYS[[length(SYS)-3L]][[1L]]) &&
37-
as.character(thisSYS) %chin% mimicsAutoPrint ) ) {
33+
if (length(SYS) <= 2L) { # "> DT" auto-print or "> print(DT)" explicit print (cannot distinguish from R 3.2.0 but that's ok)
3834
return(invisible(x))
39-
# is.symbol() temp fix for #1758.
4035
}
4136
}
4237
if (!is.numeric(nrows)) nrows = 100L
@@ -158,9 +153,6 @@ format.data.table = function(x, ..., justify="none") {
158153
do.call(cbind, lapply(x, format_col, ..., justify=justify))
159154
}
160155

161-
mimicsAutoPrint = c("knit_print.default")
162-
# add maybe repr_text.default. See https://github.com/Rdatatable/data.table/issues/933#issuecomment-220237965
163-
164156
shouldPrint = function(x) {
165157
ret = (identical(.global$print, "") || # to save address() calls and adding lots of address strings to R's global cache
166158
address(x)!=.global$print)
@@ -288,3 +280,9 @@ trunc_cols_message = function(not_printed, abbs, class, col.names){
288280
domain=NA
289281
)
290282
}
283+
284+
# Maybe add a method for repr::repr_text. See https://github.com/Rdatatable/data.table/issues/933#issuecomment-220237965
285+
knit_print.data.table <- function(x, ...) {
286+
if (!shouldPrint(x)) return(invisible(x))
287+
NextMethod()
288+
}

0 commit comments

Comments
 (0)