Skip to content

Commit 8052346

Browse files
Extend cedta for more lapply() usages (#7166)
* refactor cedta rules into helper * implement for bug * test, NEWS * About 100 packages define this now, no sense keeping track * trailing ws * rearrange: .datatable.aware to the top * push isNamespace check into helper * Fix: look up another level, use %iscall% * Revert "Fix: look up another level, use %iscall%" This reverts commit 5462a20. * Revert "push isNamespace check into helper" This reverts commit f7be165. * Revert "rearrange: .datatable.aware to the top" This reverts commit c43cee8. * Revert "trailing ws" This reverts commit 1643f36. * Revert "About 100 packages define this now, no sense keeping track" This reverts commit 885dbb1. * Revert "test, NEWS" This reverts commit d82923e. * Revert "implement for bug" This reverts commit c5e1284. * Revert "refactor cedta rules into helper" This reverts commit 41c8f67. * fix j=.SD passed to lapply '...'
1 parent 2f0d12f commit 8052346

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@
7272
7373
12. `print(..., col.names = 'none')` now correctly adapts column widths to the data content, ignoring the original column names and producing a more compact output, [#6882](https://github.com/Rdatatable/data.table/issues/6882). Thanks to @brooksambrose for the report and @venom1204 for the PR.
7474
75+
13. Reference to `.SD` in `...` arguments to `lapply()`, e.g. ``lapply(list_of_tables, `[`, j=.SD[1L])`` is evaluated correctly, [#2982](https://github.com/Rdatatable/data.table/issues/2982). Thanks @franknarf1 for the report and @MichaelChirico for the fix.
76+
7577
### NOTES
7678
7779
1. The following in-progress deprecations have proceeded:

R/cedta.R

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

42+
.any_sd_queries_in_stack = function(calls) {
43+
for (ii in length(calls):1) { # nolint: seq_linter. As above.
44+
if (!calls[[ii]] %iscall% "[") 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
# cedta = Calling Environment Data.Table-Aware
4353
cedta = function(n=2L) {
4454
# Calling Environment Data Table Aware
@@ -52,12 +62,15 @@ cedta = function(n=2L) {
5262
return(TRUE)
5363
}
5464
nsname = getNamespaceName(ns)
65+
sc = sys.calls()
5566
ans = nsname=="data.table" ||
5667
"data.table" %chin% names(getNamespaceImports(ns)) || # most common and recommended cases first for speed
5768
(nsname=="utils" &&
5869
(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
70+
(length(sc)>=8L && sc[[length(sc)-7L]] %iscall% 'example')) ) || # 'example' for #2972
71+
(nsname=="base" && # lapply
72+
(all(c("FUN", "X") %chin% ls(parent.frame(n))) ||
73+
.any_sd_queries_in_stack(sc))) ||
6174
(nsname %chin% cedta.pkgEvalsUserCode && .any_eval_calls_in_stack()) ||
6275
nsname %chin% cedta.override ||
6376
isTRUE(ns$.datatable.aware) || # As of Sep 2018: RCAS, caretEnsemble, dtplyr, rstanarm, rbokeh, CEMiTool, rqdatatable, RImmPort, BPRMeth, rlist

inst/tests/tests.Rraw

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21426,3 +21426,6 @@ test(2330.7, as.data.table(list(z), keep.rownames=TRUE), data.table(rn=rep("", 3
2142621426

2142721427
M <- matrix(1:6, nrow=3, dimnames=list(rep("", 3), c("V1", "V2"))) # test of list(M) for empty-rowname'd matrix input
2142821428
test(2330.8, as.data.table(list(M), keep.rownames=TRUE), data.table(rn=rep("", 3), V1=1:3, V2=4:6))
21429+
21430+
# .SD reference in '...' passed to lapply(FUN=) is recognized as data.table
21431+
test(2331, lapply(list(data.table(a=1:2)), `[`, j=.SD[1L]), list(data.table(a=1L)))

0 commit comments

Comments
 (0)