Quick tip on controlling log output in tests

[This article was first published on Cartesian Faith » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

When running tests for a package, it’s important that the console output is unadulterated since test results are printed with formatting. My code is littered with log output using my futile.logger package [1]. Since arbitrary appenders are supported in futile.logger, a neat trick is to redirect output to a file when running the tests. This is as simple as adding the following lines to the top of a testthat or RUnit script:

library(futile.logger)
flog.appender(appender.file("unit_tests.log"))

Now when R CMD check runs, the console output will be clean and all the log output from your package will be directed to .Rcheck//tests/unit_tests.log.

Similarly, if you want to modify the log threshold when running tests, throw in a change of threshold.

flog.threshold(DEBUG)

So you get all of this without needing to change any of the code in the package proper.

Notes

[1] The futile.logger package is available on CRAN, while the latest version is on github. To install from github, run the following from your R shell.

library(devtools)
install.packages('futile.options')
install_github('lambda.r', 'zatonovo')
install_github('futile.logger', 'zatonovo')

 


To leave a comment for the author, please follow the link and comment on their blog: Cartesian Faith » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)