1

Suppose I write a package "hello"...

R>library(devtools)
R>install_github("jimhester/covr")
R>create("hello")

...with a single exported function:

hello <- function() {
  dt<-as.data.table(list(a=1:10))
  dt[,b:=a+2]
  return(sum(dt[,b]))
}

To make this package work, I also add

Depends:
    data.table

to the DESCRIPTION file.

Now I want to write a unit test for it:

R>use_testthat("testcovr")

...in file tests/testthat/test-hello.R:

context("hello")

test_that("hello works", {
  expect_equal(hello(), 75)
})

When I run the test, they fail:

R> test()
Loading hello
Loading required package: testthat
Testing hello
hello: 1

Failed -----------------------------------------------------------------------------------------------------------------------
1. Error: hello works (@test-hello.R#7) --------------------------------------------------------------------------------------
Check that is.data.table(DT) == TRUE. Otherwise, := and `:=`(...) are defined for use in j, once only and in particular ways. See help(":=").
1: expect_equal(hello(), 75) at /home/Adama-docs/Adam/linux/tmp/hello/tests/testthat/test-hello.R:7
2: compare(object, expected, ...)
3: hello()
4: dt[, `:=`(b, a + 2)] at /home/Adama-docs/Adam/linux/tmp/hello/R/hello.R:18
5: `[.data.table`(dt, , `:=`(b, a + 2)) at /home/Adama-docs/Adam/linux/tmp/hello/R/hello.R:18
6: `[.data.frame`(x, i, j)
7: `:=`(b, a + 2)
8: stop("Check that is.data.table(DT) == TRUE. Otherwise, := and `:=`(...) are defined for use in j, once only and in particular ways. See help(\":=\").")

DONE =========================================================================================================================

Why?? Although I googled out other cases of incompatibilities between testthat and data.table, they didn't concern the := operator. And they blamed the testthat package.


Here is my session info:

Session info ------------------------------------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.3.0 (2016-05-03)
 system   x86_64, linux-gnu           
 ui       RStudio (0.99.902)          
 language en_US                       
 collate  en_US.UTF-8                 
 tz       <NA>                        
 date     2016-06-16                  

Packages ----------------------------------------------------------------------------------------------------------------------
 package    * version    date       source                          
 chron        2.3-47     2015-06-24 CRAN (R 3.3.0)                  
 crayon       1.3.1      2015-07-13 CRAN (R 3.3.0)                  
 data.table * 1.9.6      2015-09-19 CRAN (R 3.3.0)                  
 devtools   * 1.11.1     2016-04-21 CRAN (R 3.3.0)                  
 digest       0.6.9      2016-01-08 CRAN (R 3.2.4)                  
 magrittr     1.5        2014-11-22 CRAN (R 3.2.0)                  
 memoise      1.0.0      2016-01-29 CRAN (R 3.3.0)                  
 R6           2.1.2      2016-01-26 CRAN (R 3.3.0)                  
 Rcpp         0.12.5     2016-05-14 CRAN (R 3.3.0)                  
 roxygen2     5.0.1      2015-11-11 CRAN (R 3.3.0)                  
 stringi      1.1.1      2016-05-27 CRAN (R 3.3.0)                  
 stringr      1.0.0      2015-04-30 CRAN (R 3.2.0)                  
 testdt     * 0.1.0      <NA>       local                           
 testthat   * 1.0.2      2016-04-23 CRAN (R 3.3.0)                  
 withr        1.0.1.9000 2016-06-15 Github (jimhester/withr@bd42181)

Is there any workaround for it? Abandoning the data.table is not an option for me, because my library deals with really large datasets.

Adam Ryczkowski
  • 7,592
  • 13
  • 42
  • 68
  • 1
    Add data.table to imports in DESCRIPTION and also import in NAMESPACE if you've to use `testthat`. Seems like [this issue](http://stackoverflow.com/a/23279604/559784) with `testthat` hasn't been fixed yet. Also see [FAQ 6.9](https://rawgit.com/wiki/Rdatatable/data.table/vignettes/datatable-faq.html#i-have-created-a-package-that-depends-on-data.table.-how-do-i-ensure-my-package-is-data.table-aware-so-that-inheritance-from-data.frame-works). – Arun Jun 16 '16 at 20:51
  • @Arun That would be my accepted answer. I may add, that if you use ROxygen2, instead of editing `NAMESPACE` you should add a line `#' @import data.table` into your R code (the best place is where you hold documentation comments for the package as a whole) – Adam Ryczkowski Jun 17 '16 at 07:42
  • Import in NAMESPACE is required anyway. There is a [point in FAQ](https://rawgit.com/wiki/Rdatatable/data.table/vignettes/datatable-faq.html#i-have-created-a-package-that-depends-on-data.table.-how-do-i-ensure-my-package-is-data.table-aware-so-that-inheritance-from-data.frame-works) related to that, it was not yet updated in above link, you can see up to date [FAQ here](http://jangorecki.gitlab.io/data.table/library/data.table/doc/datatable-faq.html#i-have-created-a-package-that-depends-on-data.table.-how-do-i-ensure-my-package-is-data.table-aware-so-that-inheritance-from-data.frame-works). – jangorecki Jun 17 '16 at 12:43
  • @jangorecki You are right, but when you use ROxygen2, the `NAMESPACE` gets overwritten according to the `@import` tag anyway, discarding your changes. Or is it only in RStudio? – Adam Ryczkowski Jun 17 '16 at 18:32
  • @AdamRyczkowski If I use roxygen I only use it to produce Rd files, not NAMESPACE. I maintain NAMESPACE manually to have it well formatted so readers can easily see what package offers. I believe roxygen should detect imports and depends from DESCRIPTION when producing NAMESPACE - as stated in [Writing R Extensions](https://cran.r-project.org/doc/manuals/R-exts.html#Package-Dependencies) manual. – jangorecki Jun 17 '16 at 19:08
  • @jangorecki I'm curious, which question is the exact duplicate? – Adam Ryczkowski Jun 17 '16 at 19:09
  • @AdamRyczkowski most (if not all) from [here](https://stackoverflow.com/questions/tagged/data.table+testthat) which generally could point to the solution described in the question linked from here. If not exact then have the same source of problem. – jangorecki Jun 17 '16 at 19:12
  • @jangorecki Definitely not. Putting `data.table` in `imports` stanza in `DESCRIPTION` does not make ROxygen2 update `NAMESPACE`. You *need* to put the ROxygen2 comment - as stated in the book ["R Packages" by Hadley Wickham](https://www.amazon.com/dp/1491910593) ;-) – Adam Ryczkowski Jun 17 '16 at 19:17
  • @AdamRyczkowski then you should propose FR/bug fix (according to R-exts) to roxygen – jangorecki Jun 17 '16 at 19:18

0 Answers0