Skip to content

Commit effc48f

Browse files
committed
move the cedta fallback outside of [
No behaviour change.
1 parent 296435e commit effc48f

2 files changed

Lines changed: 38 additions & 14 deletions

File tree

‎R/data.table.R‎

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,24 @@ replace_dot_alias = function(e) {
521521
list(GForce=GForce, jsub=jsub, jvnames=jvnames)
522522
}
523523

524+
# [ called from a scope that is not data.table-aware (!cedta()) behaves like [.data.frame.
525+
# i, j and drop are passed on as promises, so their missingness reaches [.data.frame unchanged.
526+
# nargs is nargs() of the [.data.table call.
527+
.dt_cedta_fallback = function(x, i, j, drop, jsub, nargs, missing_i, missing_drop) {
528+
# Fix for #500 (to do)
529+
if (jsub %iscall% c(":=", "let")) {
530+
# Throw a specific error message
531+
stopf("[ was called on a data.table in an environment that is not data.table-aware (i.e. cedta()), but '%s' was used, implying the owner of this call really intended for data.table methods to be called. See vignette('datatable-importing') for details on properly importing data.table.", as.character(jsub[[1L]]))
532+
}
533+
Nargs = nargs - (!missing_drop)
534+
ans = if (Nargs<3L) { `[.data.frame`(x,i) } # drop ignored anyway by DF[i]
535+
else if (missing_drop) `[.data.frame`(x,i,j)
536+
else `[.data.frame`(x,i,j,drop)
537+
# added is.data.table(ans) check to fix bug #81
538+
if (!missing_i && is.data.table(ans)) setkey(ans, NULL) # drops index too; tested by plyr::arrange test in other.Rraw
539+
ans
540+
}
541+
524542
"[.data.table" = function(x, i, j, by, keyby, with=TRUE, nomatch=NA, mult="all", roll=FALSE, rollends=if (roll=="nearest") c(TRUE,TRUE) else if (roll>=0.0) c(FALSE,TRUE) else c(TRUE,FALSE), which=FALSE, .SDcols, verbose=getOption("datatable.verbose"), allow.cartesian=getOption("datatable.allow.cartesian"), drop=NULL, on=NULL, env=NULL, showProgress=getOption("datatable.showProgress", interactive()))
525543
{
526544
# ..selfcount <<- ..selfcount+1 # in dev, we check no self calls, each of which doubles overhead, or could
@@ -539,20 +557,8 @@ replace_dot_alias = function(e) {
539557
missing_verbose = missing(verbose)
540558
# capture parent.frame() changes once as calling scope
541559
callenv = parent.frame()
542-
if (!cedta()) {
543-
# Fix for #500 (to do)
544-
if (substitute(j) %iscall% c(":=", "let")) {
545-
# Throw a specific error message
546-
stopf("[ was called on a data.table in an environment that is not data.table-aware (i.e. cedta()), but '%s' was used, implying the owner of this call really intended for data.table methods to be called. See vignette('datatable-importing') for details on properly importing data.table.", as.character(substitute(j)[[1L]]))
547-
}
548-
Nargs = nargs() - (!missing_drop)
549-
ans = if (Nargs<3L) { `[.data.frame`(x,i) } # drop ignored anyway by DF[i]
550-
else if (missing_drop) `[.data.frame`(x,i,j)
551-
else `[.data.frame`(x,i,j,drop)
552-
# added is.data.table(ans) check to fix bug #81
553-
if (!missing_i && is.data.table(ans)) setkey(ans, NULL) # drops index too; tested by plyr::arrange test in other.Rraw
554-
return(ans)
555-
}
560+
# cedta() and nargs() depend on this frame so are evaluated here
561+
if (!cedta()) return(.dt_cedta_fallback(x, i, j, drop, substitute(j), nargs(), missing_i, missing_drop))
556562
if (!missing_verbose) {
557563
if (!is.integer(verbose) && !is.logical(verbose)) stopf("verbose must be logical or integer")
558564
if (length(verbose)!=1L || anyNA(verbose)) stopf("verbose must be length 1 non-NA")

‎inst/tests/bracket.Rraw‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,21 @@ test(2.48, X[, lapply(.SD, sum), .SDcols=is.integer], data.table(id=7L, v=10L))
171171
test(2.49, X[, lapply(.SD, sum), .SDcols=!"id"], data.table(t=12, v=10L))
172172
test(2.50, X[, lapply(.SD, sum), .SDcols=-(id:t)], data.table(v=10L))
173173
test(2.51, X[, lapply(.SD, function(x) x/v), .SDcols=!"v"], data.table(id=c(1,0.5,2/3,0.75), t=c(1,2.5,2/3,1)))
174+
175+
# [ from a scope that is not data.table-aware dispatches to [.data.frame, #852
176+
DT = data.table(a=1:3, b=c("x","y","z"), key="b")
177+
DF = as.data.frame(DT)
178+
naive = function(expr) eval(substitute(expr), list(DT=DT), new.env(parent=asNamespace("base"))) # use base as namespace to be sure its installed
179+
# results of [.data.frame are not over-allocated, hence compared as data.frame
180+
test(3.01, setDF(naive(DT[2])), DF[2])
181+
test(3.02, setDF(naive(DT[1:2,])), DF[1:2,]) # slice from front for rownames
182+
test(3.03, setDF(naive(DT[, 2, drop=FALSE])), DF[, 2, drop=FALSE])
183+
test(3.04, naive(DT[, 2]), DF[, 2])
184+
test(3.05, naive(DT[2, "a"]), DF[2, "a"])
185+
test(3.06, key(naive(DT[])), "b") # key retained when i is missing, dropped otherwise
186+
test(3.07, key(naive(DT[1:3, ])), NULL)
187+
test(3.08, naive(DT[, a := 1L]), error="[ was called on a data.table in an environment that is not data.table-aware (i.e. cedta()), but ':=' was used")
188+
test(3.09, naive(DT[, let(a = 1L)]), error="but 'let' was used")
189+
test(3.10, class(naive(DT[2:3, ])), c("data.table","data.frame"))
190+
test(3.11, tryCatch(naive(DT[, a := 1L]), error=conditionCall)[[1L]], quote(`[.data.table`)) # not .dt_cedta_fallback
191+
rm(DT, DF, naive)

0 commit comments

Comments
 (0)