Skip to content

Commit 41c8f67

Browse files
refactor cedta rules into helper
1 parent 2f0d12f commit 41c8f67

File tree

1 file changed

+32
-12
lines changed

1 file changed

+32
-12
lines changed

R/cedta.R

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,36 @@ cedta.pkgEvalsUserCode = c("gWidgetsWWW","statET","FastRWeb","slidify","rmarkdow
3939
}
4040
# nocov end
4141

42+
# in a helper to promote readability
43+
# NB: put the most common and recommended cases first for speed
44+
.cedta_impl_ <- function(ns, n) {
45+
nsname = getNamespaceName(ns)
46+
if (nsname == "data.table") return(TRUE)
47+
48+
if ("data.table" %chin% names(getNamespaceImports(ns))) return(TRUE)
49+
50+
if (nsname == "utils") {
51+
if (exists("debugger.look", parent.frame(n+1L))) return(TRUE)
52+
53+
# 'example' for #2972
54+
sc <- sys.calls()
55+
if (length(sc) >= 8L && sc[[length(sc) - 7L]] %iscall% 'example') return(TRUE)
56+
}
57+
58+
if (nsname == "base" && all(c("FUN", "X") %chin% ls(parent.frame(n)))) return(TRUE) # lapply
59+
60+
if (nsname %chin% cedta.pkgEvalsUserCode && .any_eval_calls_in_stack()) return(TRUE)
61+
62+
if (nsname %chin% cedta.override) return(TRUE)
63+
64+
# As of Sep 2018: RCAS, caretEnsemble, dtplyr, rstanarm, rbokeh, CEMiTool, rqdatatable, RImmPort, BPRMeth, rlist
65+
if (isTRUE(ns$.datatable.aware)) return(TRUE)
66+
67+
# both ns$.Depends and get(.Depends,ns) are not sufficient
68+
pkg_ns = paste("package", nsname, sep=":")
69+
tryCatch("data.table" %chin% get(".Depends", pkg_ns, inherits=FALSE), error=function(e) FALSE)
70+
}
71+
4272
# cedta = Calling Environment Data.Table-Aware
4373
cedta = function(n=2L) {
4474
# Calling Environment Data Table Aware
@@ -51,20 +81,10 @@ cedta = function(n=2L) {
5181
# e.g. DT queries at the prompt (.GlobalEnv) and knitr's eval(,envir=globalenv()) but not DF[...] inside knitr::kable v1.6
5282
return(TRUE)
5383
}
54-
nsname = getNamespaceName(ns)
55-
ans = nsname=="data.table" ||
56-
"data.table" %chin% names(getNamespaceImports(ns)) || # most common and recommended cases first for speed
57-
(nsname=="utils" &&
58-
(exists("debugger.look", parent.frame(n+1L)) ||
59-
(length(sc<-sys.calls())>=8L && sc[[length(sc)-7L]] %iscall% 'example')) ) || # 'example' for #2972
60-
(nsname=="base" && all(c("FUN", "X") %chin% ls(parent.frame(n)))) || # lapply
61-
(nsname %chin% cedta.pkgEvalsUserCode && .any_eval_calls_in_stack()) ||
62-
nsname %chin% cedta.override ||
63-
isTRUE(ns$.datatable.aware) || # As of Sep 2018: RCAS, caretEnsemble, dtplyr, rstanarm, rbokeh, CEMiTool, rqdatatable, RImmPort, BPRMeth, rlist
64-
tryCatch("data.table" %chin% get(".Depends",paste("package",nsname,sep=":"),inherits=FALSE),error=function(e)FALSE) # both ns$.Depends and get(.Depends,ns) are not sufficient
84+
ans = .cedta_impl_(ns, n)
6585
if (!ans && getOption("datatable.verbose")) {
6686
# nocov start
67-
catf("cedta decided '%s' wasn't data.table aware. Here is call stack with [[1L]] applied:\n", nsname)
87+
catf("cedta decided '%s' wasn't data.table aware. Here is call stack with [[1L]] applied:\n", getNamespaceName(ns))
6888
print(sapply(sys.calls(), `[[`, 1L))
6989
# nocov end
7090
# so we can trace the namespace name that may need to be added (very unusually)

0 commit comments

Comments
 (0)