diff --git a/NEWS.md b/NEWS.md index 732ad85680..84370cf098 100644 --- a/NEWS.md +++ b/NEWS.md @@ -85,6 +85,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 fa3ef6784b..db10ee4a88 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -14459,6 +14459,14 @@ test(2035.1, fread('A,B\n"foo","ba"r"', quote="''"), error='quote= must be a sin test(2035.2, fread('A,B\n"foo","ba"r"', quote=FALSE), ans<-data.table(A='"foo"', B='"ba"r"')) test(2035.3, fread('A,B\n"foo","ba"r"', quote=""), ans) +# source() printing edge case; #2369 +setup = c('library(data.table)', 'DT = data.table(a = 1)') +writeLines(c(setup, 'DT[ , a := 1]'), tmp<-tempfile()) +test(2036.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(2036.2, source(tmp, echo = TRUE), output = "1:\\s+1") + ################################### # Add new tests above this line #