As an experienced data.table user, I can generally recognize the cause of the := error message
Check that is.data.table(DT) == TRUE. Otherwise, := and :=(...) are defined for use in j, once only and in particular ways. See help(":=").
However, (a) it still takes a bit of work, and (b) it doesn't fully diagnose the problem, even though it's possible. The following is a proof-of-concept that provides slightly better advice for the common error of providing an update expression to i = instead of j = .
Obviously the performance is irrelevant as it is only going to error at this point.
library(data.table)
DT<- as.data.table(mtcars)
DT[mpg_per_cyl:=mpg/cyl]
#> Error: Check that is.data.table(DT) == TRUE. Otherwise, := and `:=`(...) are defined for use in j, once only and in particular ways. See help(":=").":="<-function(x, y) {
.trace_back<-rlang::trace_back()
if (length(.trace_back) >=2L&&
length(.trace_back[[2]]) >=3L&&
as.character(.trace_back[[1L]][[2]][[3]][[1]]) ==":=") {
stop(":= is intended for use in j only, but is present in i. ",
"(Did you forget to put a comma ",
"before the expression?)")
} else {
stop("Check that is.data.table(DT) == TRUE. Otherwise, := and `:=`(...) are defined for use in j, once only and in particular ways. See help(\":=\").")
}
}
DT[mpg_per_cyl:=mpg/cyl]
#> Error: := is intended for use in j only, but is present in i. (Did you forget to put a comma before the expression?)
As an experienced
data.table
user, I can generally recognize the cause of the:=
error messageHowever, (a) it still takes a bit of work, and (b) it doesn't fully diagnose the problem, even though it's possible. The following is a proof-of-concept that provides slightly better advice for the common error of providing an update expression to
i =
instead ofj =
.Obviously the performance is irrelevant as it is only going to error at this point.
Created on 2020-02-03 by the reprex package (v0.3.0)
The text was updated successfully, but these errors were encountered: