user3312158
user3312158

Reputation: 11

How to test standalone R scripts which are part of a package using testthat

I am building a R package, RE and I would like to package a standalone script with it to be placed in RE/exec/wrapper.R. I have written a test function for testthat which works just fine when run from the command line using devtools::test("RE"), but fails when run using devtools::check("RE").

The relevant code portion that fails is:

cmd <- "Rscript \"C:\\RE\\exec\\wrapper.R\" base print \"{\\\"x\\\": 2}\""
rv <- system(cmd, intern = FALSE, ignore.stderr = FALSE, show.output.on.console = FALSE)

When run as part of "test" everything runs fine and system returns 0 as it should. When run as part of "check", system gives out the error message given below and returns 1.

Error in file(filename, "r", encoding = encoding) : cannot open the connection
Calls: local ... eval.parent -> eval -> eval -> eval -> eval -> source -> file
In addition: Warning message:
In file(filename, "r", encoding = encoding) : cannot open file 'startup.Rs': No such file or directory
Execution halted

I have:

  1. Checked that the file exists (it does in both cases)
  2. Tried tryCatch. No error are caught, and system just returns a 1

This makes me think that it is an issue with system and R CMD CHECK.

Upvotes: 0

Views: 392

Answers (1)

user3312158
user3312158

Reputation: 11

The answer is in https://github.com/r-lib/testthat/issues/144 . The environment variable R_TESTS must be unset via Sys.setenv("R_TESTS" = "") in the top level test script (package_root/tests/testthat.R), or system will not run correctly when called from R CMD CHECK. I am leaving the question up, because it took me several hours of experimenting and searching before stumbling on the answer.

Upvotes: 1

Related Questions