I'm learning to do my first unit tests with R, and I write my code in R Markdown files to make delivering short research reports easy. At the same time, I would like to test the functions I use in these files to make sure the results are sane.
Here's the problem: R Markdown files are meant to go into HTML weavers, not the RUnit test harness. If I want to load a function into the test code, I have a few choices:
Is there a more sensible way to go about this that avoids the disadvantages of each of these approaches?
You could do something like this
## Rmarkdown file with tests
```{r definefxn}
foo <- function(x) x^2
```
Test fxn
```{r testfxn}
library(testthat)
expect_is(foo(8), "numeric")
expect_equal(foo(8), 6)
```
Where of course the tests that pass don't print anything, but the tests that fail print meaningful messages about what failed.