Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the error message for wrongly-placed := #4227

Closed
HughParsonage opened this issue Feb 3, 2020 · 0 comments · Fixed by #4228
Closed

Improve the error message for wrongly-placed := #4227

HughParsonage opened this issue Feb 3, 2020 · 0 comments · Fixed by #4228
Milestone

Comments

@HughParsonage
Copy link
Member

@HughParsonage HughParsonage commented Feb 3, 2020

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?)

Created on 2020-02-03 by the reprex package (v0.3.0)

@mattdowle mattdowle added this to the 1.12.9 milestone Feb 15, 2020
@jangorecki jangorecki removed this from the 1.12.11 milestone May 26, 2020
@jangorecki jangorecki added this to the 1.12.9 milestone May 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants