Skip to content

Commit

Permalink
catch unsupported use of .I in by
Browse files Browse the repository at this point in the history
  • Loading branch information
mattdowle committed Dec 9, 2021
1 parent 3a4cfdd commit e136746
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
7 changes: 3 additions & 4 deletions R/data.table.R
Original file line number Diff line number Diff line change
Expand Up @@ -766,10 +766,9 @@ replace_dot_alias = function(e) {
# may evaluate to NULL | character() | "" | list(), likely a result of a user expression where no-grouping is one case being loop'd through
bysubl = as.list.default(bysub)
bysuborig = bysub

# support .I in by #1732
# use %in% to support by=.I, by=.(.I), by=c(.I) and by=list(.I)
if (".I" %in% bysubl) {
if (".I" %in% bysubl) { #1732
if (!is.symbol(bysub) && (length(bysubl)!=2L || !is.symbol(bysubl[[2L]]) || !(bysubl[[1L]] %chin% c(".","c","list"))))
stopf("'by' contains .I but only the following are currently supported: by=.I, by=.(.I), by=c(.I), by=list(.I)")
bysub = if (is.null(irows)) seq_len(nrow(x)) else irows
bysuborig = as.symbol("I")
}
Expand Down
6 changes: 4 additions & 2 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -18688,8 +18688,8 @@ ydt[, ret := rnorm(.N)]
f = shift
test(2233.33, copy(ydt)[, (ycols) := shift(ret, yn, type = "lead"), by = symbol, verbose=TRUE], copy(ydt)[, (ycols) := f(ret, yn, type = "lead"), by = symbol], output="GForce optimized j to")

# support by=.I #1732
DT <- data.table(V1=1:5, V2=3:7, V3=5:1)
# support by=.I; #1732
DT = data.table(V1=1:5, V2=3:7, V3=5:1)
test(2234.1, DT[, min(.SD), by=.I], setnames(DT[, min(.SD), by=1:nrow(DT)], "nrow", "I"))
test(2234.2, DT[, min(.SD), by=.I], data.table(I=1L:5L, V1=c(1L, 2L, 3L, 2L, 1L)))
# works also with i
Expand All @@ -18699,4 +18699,6 @@ test(2234.4, DT[c(4, NA), min(.SD), by=.I], data.table(I=c(4L, NA), V1=c(2L, NA)
test(2234.5, DT[, min(.SD), by=.(.I)], data.table(I=1L:5L, V1=c(1L, 2L, 3L, 2L, 1L)))
test(2234.6, DT[, min(.SD), by=list(.I)], data.table(I=1L:5L, V1=c(1L, 2L, 3L, 2L, 1L)))
test(2234.7, DT[, min(.SD), by=c(.I)], data.table(I=1L:5L, V1=c(1L, 2L, 3L, 2L, 1L)))
test(2234.8, DT[, min(.SD), by=.I%%2L], error="by.*contains .I.*supported") # would be nice to support in future; i.e. by odd/even rows, and by=(.I+1L)%/%2L for pairs of rows; i.e. any expression of .I
test(2234.9, DT[, min(.SD), by=somefun(.I)], error="by.*contains .I.*supported")

0 comments on commit e136746

Please sign in to comment.