@@ -28,8 +28,7 @@ cedta.pkgEvalsUserCode = c("gWidgetsWWW","statET","FastRWeb","slidify","rmarkdow
2828
2929# nocov start: very hard to reach this within our test suite -- the call stack within a call generated by e.g. knitr
3030# for loop, not any(vapply_1b(.)), to allow early exit
31- .any_eval_calls_in_stack = function () {
32- calls = sys.calls()
31+ .any_eval_calls_in_stack = function (calls ) {
3332 # likelier to be close to the end of the call stack, right?
3433 for (ii in length(calls ): 1 ) { # nolint: seq_linter. rev(seq_len(length(calls)))? See https://bugs.r-project.org/show_bug.cgi?id=18406.
3534 the_call = calls [[ii ]][[1L ]]
@@ -39,6 +38,17 @@ cedta.pkgEvalsUserCode = c("gWidgetsWWW","statET","FastRWeb","slidify","rmarkdow
3938}
4039# nocov end
4140
41+ .any_sd_queries_in_stack = function (calls ) {
42+ for (ii in length(calls ): 1 ) { # nolint: seq_linter. As above.
43+ the_call = calls [[ii ]][1L ]
44+ if (! is.name(the_call ) || the_call != " [" ) next
45+ the_lhs = calls [[ii ]][[2L ]]
46+ if (! is.name(the_lhs ) || the_lhs != " .SD" ) next
47+ return (TRUE )
48+ }
49+ FALSE
50+ }
51+
4252# in a helper to promote readability
4353# NB: put the most common and recommended cases first for speed
4454.cedta_impl_ <- function (ns , n ) {
@@ -47,17 +57,21 @@ cedta.pkgEvalsUserCode = c("gWidgetsWWW","statET","FastRWeb","slidify","rmarkdow
4757
4858 if (" data.table" %chin % names(getNamespaceImports(ns ))) return (TRUE )
4959
60+ sc <- sys.calls()
5061 if (nsname == " utils" ) {
5162 if (exists(" debugger.look" , parent.frame(n + 1L ))) return (TRUE )
5263
5364 # 'example' for #2972
54- sc <- sys.calls()
5565 if (length(sc ) > = 8L && sc [[length(sc ) - 7L ]] %iscall % ' example' ) return (TRUE )
5666 }
5767
58- if (nsname == " base" && all(c(" FUN" , " X" ) %chin % ls(parent.frame(n )))) return (TRUE ) # lapply
68+ if (nsname == " base" ) {
69+ if (all(c(" FUN" , " X" ) %chin % ls(parent.frame(n )))) return (TRUE ) # lapply
70+
71+ if (.any_sd_queries_in_stack(sc )) return (TRUE ) # e.g. lapply() where "piped-in" j= arg has .SD[]
72+ }
5973
60- if (nsname %chin % cedta.pkgEvalsUserCode && .any_eval_calls_in_stack()) return (TRUE )
74+ if (nsname %chin % cedta.pkgEvalsUserCode && .any_eval_calls_in_stack(sc )) return (TRUE )
6175
6276 if (nsname %chin % cedta.override ) return (TRUE )
6377
0 commit comments