Search code examples
runit-testingdevtoolsr-packagetestthat

Where to put test data in R package for R CMD CHECK


This question is a near duplicate of Q32328802 - testdata for testthat. The difference is that I'm looking for a more specific answer as it relates to devtools::check. Specifically, how do I get devtools::check() to recognize and load my test data?

My package structure for tests/testthat.R and tests/testthat/setup-testdata.R is described below (and github is linked at the bottom of the post). I have tried the following:

  • Following the above linked question, I have placed the test data in inst/testdata. As this is not working, I have also tried other locations (eg. tests/, tests/testthat). None of these locations work.
  • I have also tried renaming helper-testdata.R as setup-testdata.R and get the same failing results.
  • devtools::check() does not appear to accurately recognize the load(system.file(...)) command. It similarly does not recognize if I spell the file name out (ie. do not use system.file()). Omitting the load command also doesn't work.

tests/:

testthat.R

library(synthACS)
library(testthat)

test_check("synthACS")

testthat/setup-testdata.R has a single line in it:

load(system.file("testdata", 'dat-acsdata.Rda', package= "synthACS"))
# run interactively, this line of code loads the data accurately.
# within devtools::check() it appears to return an empty string ("") for
# file location

Checking my package

R> devtools::test(synthACS)
══ Results ═════════════════════════

OK:       388
Failed:   0
Warnings: 0
Skipped:  0

R> devtools::check(synthACS)
...
─  checking tests ...
E  Running ‘testthat.R’ (1.5s)

── Test failures ─────────────────────

> library(testthat)
> 
> load(system.file("testdata", 'acsdata.rda', package= "synthACS"))
> test_check("synthACS")
 ----------- FAILURE REPORT -------------- 
... indicates that the data is not loaded ...

Any help appreciated!

Edit links to full code:


Solution

  • The correct answer is to use readRDS, which means saving each object separately via saveRDS

    eg.

    my_data <- readRDS(file= system.file("testdata", 'dat-mydata.rds', package= "synthACS"))