Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve the knitr auto-printing problem by registering a method for knit_print #6589

Merged
merged 12 commits into from
Dec 5, 2024
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ if (getRversion() >= "4.0.0") {
# version of R (and that is checked in .onLoad with error if not).
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
}
if (getRversion() >= "3.6.0") S3method(knitr::knit_print, data.table)
MichaelChirico marked this conversation as resolved.
Show resolved Hide resolved
# else manual delayed registration from the onLoad hook
aitap marked this conversation as resolved.
Show resolved Hide resolved
S3method(dim, data.table)
S3method(dimnames, data.table)
S3method("dimnames<-", data.table)
Expand Down
10 changes: 10 additions & 0 deletions R/onLoad.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@
lockBinding("rbind.data.frame",baseenv())
}
}
if (session_r_version < "3.6.0") {
aitap marked this conversation as resolved.
Show resolved Hide resolved
# no delayed registration support for NAMESPACE; perform it manually
if (isNamespaceLoaded("knitr")) {
MichaelChirico marked this conversation as resolved.
Show resolved Hide resolved
registerS3method("knit_print", "data.table", knit_print.data.table, envir = asNamespace("knitr"))
} else {
setHook(packageEvent("knitr", "onLoad"), function(...) {
registerS3method("knit_print", "data.table", knit_print.data.table, envir = asNamespace("knitr"))
})
}
}

# Set options for the speed boost in v1.8.0 by avoiding 'default' arg of getOption(,default=)
# In fread and fwrite we have moved back to using getOption's default argument since it is unlikely fread and fread will be called in a loop many times, plus they
Expand Down
16 changes: 7 additions & 9 deletions R/print.data.table.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,8 @@ print.data.table = function(x, topn=getOption("datatable.print.topn"),
# Other options investigated (could revisit): Cstack_info(), .Last.value gets set first before autoprint, history(), sys.status(),
# topenv(), inspecting next statement in caller, using clock() at C level to timeout suppression after some number of cycles
SYS = sys.calls()
if (length(SYS) <= 2L || # "> DT" auto-print or "> print(DT)" explicit print (cannot distinguish from R 3.2.0 but that's ok)
( length(SYS) >= 3L && is.symbol(thisSYS <- SYS[[length(SYS)-2L]][[1L]]) &&
as.character(thisSYS) == 'source') || # suppress printing from source(echo = TRUE) calls, #2369
MichaelChirico marked this conversation as resolved.
Show resolved Hide resolved
( length(SYS) > 3L && is.symbol(thisSYS <- SYS[[length(SYS)-3L]][[1L]]) &&
as.character(thisSYS) %chin% mimicsAutoPrint ) ) {
if (length(SYS) <= 2L) { # "> DT" auto-print or "> print(DT)" explicit print (cannot distinguish from R 3.2.0 but that's ok)
return(invisible(x))
# is.symbol() temp fix for #1758.
}
}
if (!is.numeric(nrows)) nrows = 100L
Expand Down Expand Up @@ -158,9 +153,6 @@ format.data.table = function(x, ..., justify="none") {
do.call(cbind, lapply(x, format_col, ..., justify=justify))
}

mimicsAutoPrint = c("knit_print.default")
# add maybe repr_text.default. See https://github.com/Rdatatable/data.table/issues/933#issuecomment-220237965

shouldPrint = function(x) {
ret = (identical(.global$print, "") || # to save address() calls and adding lots of address strings to R's global cache
address(x)!=.global$print)
Expand Down Expand Up @@ -288,3 +280,9 @@ trunc_cols_message = function(not_printed, abbs, class, col.names){
domain=NA
)
}

# Maybe add a method for repr::repr_text. See https://github.com/Rdatatable/data.table/issues/933#issuecomment-220237965
knit_print.data.table <- function(x, ...) {
if (!shouldPrint(x)) return(invisible(x))
NextMethod()
}