I have created a markdown script which is producing some ggplot
graphs. I also wrote a test script to automatically testing the whole markdown by first extracting the code chunks and then executing the script.
With some test data, I get warnings like Removed 1 rows containing missing values (geom_point)
which I normally ignore and since updating testthat
to 1.0.0, it is complaining about these warnings because I don't "expect" them.
runAllChunks <- function(rmd, envir=globalenv()){
# as found here http://stackoverflow.com/questions/24753969
tempR <- tempfile(tmpdir = '.', fileext = ".R")
on.exit(unlink(tempR))
knitr::purl(rmd, output=tempR, quiet=TRUE)
sys.source(tempR, envir=envir)
}
expect_that(f <- runAllChunks('analysis.Rmd'), not(throws_error()))
testthat
is reporting the following:
testing analysis script: WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW1.............
Warnings ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1. analysis works with "nurias_testdata" folder. (@test_analysis.R#48) - Removed 1164 rows containing non-finite values (stat_boxplot).
... truncated here ...
In older versions of testthat
I got rid of the warnings by saving the result into f
. But now testing throws warning messages and also the not
is deprecated. I changes this to check for expect_success
, but with the same result.
expect_success(f <- runAllChunks('analysis.Rmd'))
So not sure how to test my script and take care of the warnings. I wouldn't like to suppress all warnings inside the markdown, also to always filter before plotting is a bit exhausting.
Update
Thanks to comment ov bVa I fixed my geom_*
sections in the plot to include na.rm=TRUE
and got rid of the messages.
I am now just wondering, if I expect multiple messages or warning while testing a markdown script, how can I check for those without running the markdown chunks several times? Is there a way of doing this?
I am asking because I have now still a warning message I expect, but I would also would love to check for success with expect_success
. Or is the way to go by using complicated regex like
expect_warning(f <- runAllChunks('analysis.Rmd'), 'warningA|warningB|warningC')