With Roxygen and testthat, what is the proper way to make internal helper functions available to testcases called during R CMD check?
Asked Answered
T

1

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.)

Trabeated answered 9/10, 2013 at 18:11 Comment(1)
Can't you just use yourPackage:::helperfunction1(... there? (Note, that these are 3 not 2 colons.)Rodriguez
T
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.

Trash answered 10/10, 2013 at 4:39 Comment(2)
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")Trabeated
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.Trash

© 2022 - 2024 — McMap. All rights reserved.