Quick tip on controlling log output in tests
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
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')
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.