3

I am creating an R package, and found it useful to break parts of the logic in one file into internal helper functions, which I define in the same file. I have sort of a special case where my function decides which helper function to use via match.fun(). Since they won't be useful to other functions or people, I don't want to put these in separate files, and I don't want to export them.

All my testthat cases pass using test_dir(). When I don't export these functions, my testthat cases fail during R CMD check.

"object 'helperfunction1' of mode 'function' was not found", quote(get(as.character(FUN),
         mode = "function", envir = envir)))

After looking at this post, I am able to get things to work if I explicitly export or add export entries to NAMESPACE, but again I don't want to export these.

Is there a better way to do this and doesn't require me to export? (I'll admit that the source of the issue may be match.fun() and am open to other ways of calling functions at runtime.)

Community
  • 1
  • 1
Alex Benke
  • 153
  • 1
  • 8
  • Can't you just use `yourPackage:::helperfunction1(...` there? (Note, that these are 3 not 2 colons.) – jan-glx Dec 07 '16 at 17:14

1 Answers1

1

From memory it wasn't clear in the documentation last time I read it (it may have changed), but it will work correctly (without having to export) so long as everything is in the right directories:

You should have a file:

tests/run-all.R

That looks like:

library(testthat)
library(myPackage)

test_package("myPackage")

Then your individual test files should be in the directory inst/tests

These will be run when you do R CMD check, otherwise you can call test_package("myPackage") in R manually.

Scott Ritchie
  • 10,293
  • 3
  • 28
  • 64
  • Yes I have that test runner, but in this case I'm not planning to make my tests available in the package, so they aren't in inst/tests. Is there another way around this? As of now my test runner (`tests/run-all.R`) looks like: `library(testthat) test_dir("testthat")` – Alex Benke Oct 14 '13 at 20:09
  • I'm not sure. I just know that if the files are in `inst/tests` then you don't need to export the functions those tests rely on. – Scott Ritchie Oct 14 '13 at 23:51