0

The following runs fine in a console:

library(data.table)
dt = data.table(x = 1:5)
dt[, y := 1]

But if I include these lines in an a package, and load it with devtools::load_all("."), I get this error:

Error in `:=`(y, 1) : 
  Check that is.data.table(DT) == TRUE. Otherwise, := and `:=`(...) are defined for use in j, once only and in particular ways. See help(":=").

Obviously, data.table is imported since it doesn't fail at the second line. So what's wrong here, and how can I make it run?

The package was created via usethis::create_package() and it only has these three lines of code. This is the most minimal repex I could make. I get the same error if I put the code inside a function and load data.table via roxygen2 ("' @import data.table). I do have data.table (>= 1.14.0) in DESCRIPTION Imports.

The error has the following trace:

14. stop("Check that is.data.table(DT) == TRUE. Otherwise, := and `:=`(...) are defined for use in j, once only and in particular ways.
See help(\":=\").") 
13. `:=`(y, 1) 
12. `[.data.frame`(x, i, j) 
11. `[.data.table`(dt, , `:=`(y, 1)) at tmp.R#3
10. dt[, y := 1] at tmp.R#3
9. eval(exprs[i], envir) 
8. eval(exprs[i], envir) 
7. source_one(file, encoding, envir = envir) 
6. source_many(paths, encoding, env) 
5. force(code) 
4. withr_with_dir(path, source_many(paths, encoding, env)) 
3. load_code(path) 
2. pkgload::load_all(path = path, reset = reset, recompile = recompile, 
    export_all = export_all, helpers = helpers, quiet = quiet, 
    ...) 
1. devtools::load_all(".")  

sessionInfo():

R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] data.table_1.14.0

loaded via a namespace (and not attached):
 [1] rstudioapi_0.13   magrittr_2.0.1    usethis_2.0.1     devtools_2.3.2    pkgload_1.2.0    
 [6] R6_2.5.0          rlang_0.4.10      fastmap_1.1.0     tools_4.0.3       pkgbuild_1.2.0   
[11] xfun_0.21         sessioninfo_1.1.1 tinytex_0.30      cli_2.3.1         withr_2.4.1      
[16] ellipsis_0.3.1    remotes_2.2.0     assertthat_0.2.1  rprojroot_2.0.2   lifecycle_1.0.0  
[21] crayon_1.4.1      processx_3.4.5    purrr_0.3.4       callr_3.5.1       fs_1.5.0         
[26] ps_1.6.0          testthat_3.0.2    memoise_2.0.0     glue_1.4.2        cachem_1.0.4     
[31] compiler_4.0.3    desc_1.3.0        prettyunits_1.1.1
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
Jonas Lindeløv
  • 5,442
  • 6
  • 31
  • 54
  • Does this answer your question? [Using data.table package inside my own package](https://stackoverflow.com/questions/10527072/using-data-table-package-inside-my-own-package) – qdread Mar 14 '21 at 01:34

1 Answers1

1

Solution: add this somewhere in your package using roxygen2:

#' @importFrom data.table ":="
NULL

This will write importFrom(data.table,":=") to your NAMESPACE file.

Jonas Lindeløv
  • 5,442
  • 6
  • 31
  • 54