From 6fc82e1c45f7a601d4e10fb30a41db672331125b Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Sun, 5 May 2019 02:47:15 +0800 Subject: [PATCH] Closes #2369 -- edge case of induced printing from source() --- NEWS.md | 2 ++ R/print.data.table.R | 2 ++ inst/tests/tests.Rraw | 8 ++++++++ 3 files changed, 12 insertions(+) diff --git a/NEWS.md b/NEWS.md index 521ab3907a..2d0d08710f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -77,6 +77,8 @@ 5. `fread()` could crash if `quote=""` (i.e. ignore quotes), the last line is too short, and `fill=TRUE`, [#3524](https://github.com/Rdatatable/data.table/pull/3524). Thanks to Jiucang Hao for the report and reproducible example. +6. Printing could occur unexpectedly when code is run with `source`, [#2369](https://github.com/Rdatatable/data.table/issues/2369). Thanks to @jan-glx for the report and reproducible example. + #### NOTES 1. `rbindlist`'s `use.names="check"` now emits its message for automatic column names (`"V[0-9]+"`) too, [#3484](https://github.com/Rdatatable/data.table/pull/3484). See news item 5 of v1.12.2 below. diff --git a/R/print.data.table.R b/R/print.data.table.R index 6edf0d1954..7b73928c42 100644 --- a/R/print.data.table.R +++ b/R/print.data.table.R @@ -25,6 +25,8 @@ print.data.table <- function(x, topn=getOption("datatable.print.topn"), # 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 ( length(SYS) > 3L && is.symbol(thisSYS <- SYS[[length(SYS)-3L]][[1L]]) && as.character(thisSYS) %chin% mimicsAutoPrint ) ) { return(invisible(x)) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index cd97c0cfc2..551e53f465 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -14370,6 +14370,14 @@ if (test_yaml) { # csvy; #1701 DT_yaml, warning = 'Combining a search.*YAML.*') } +# #2369 -- source()d data.table printing edge case +tmp = tempfile() +setup = c('library(data.table)', 'DT = data.table(a = 1)') +writeLines(c(setup, 'DT[ , a := 1]'), tmp) +test(2033.1, !any(grepl("1: 1", capture.output(source(tmp, echo = TRUE)), fixed = TRUE))) +## test force-printing still works +writeLines(c(setup, 'DT[ , a := 1][]'), tmp) +test(2033.2, source(tmp, echo = TRUE), output = "1:\\s+1") ################################### # Add new tests above this line #