I am playing around with Rmarkdown
and assertthat
to produce a number of .pdf files in a loop. Each has a assert chunk
, with the option set to include=FALSE
.
I have a file to deliberately succeed with this chunk:
assert_that(are_equal(length(cars), length(cars)), msg = "Wrong length")
And a file to deliberately error with this chunk:
assert_that(are_equal(length(cars), 1), msg = "Wrong length")
I also have a file that I am trying to generate a warning from, with this chunk:
validate_that(are_equal(length(cars), 1), msg = "Wrong length")
I loop through these in this way:
library(loggr)
log_file("./assert_test.log")
files <- c("./success.Rmd", "./warn.Rmd", "./error.Rmd")
for(this_file in files){
tryCatch(
rmarkdown::render(
input = this_file,
output_file = paste0(this_file, ".pdf")
),
error = function(cond) {
log_info(paste("Error for:", this_file))
log_info("Here's the original error message: ")
log_info(cond)
return(NA)
},
warning = function(cond) {
log_info(paste("Warning for:", this_file))
log_info("Here's the original warning message: ")
log_info(cond)
return(NULL)
},
finally = {
log_info(paste("Finish processing for:", this_file))
}
)
}
- The
success.Rmd
correctly produces an output, theassert chunk
is not visible in the output, and does not print it's return. - The
error.Rmd
(also correctly) does not produce an output, but does produce an error message, which is captured inassert_test.log
. - I would like the
warn.Rmd
to generate a .pdf, with the output not showing any evidence that theassert chunk
is present (no code or return in the document), but also to pass the warning up to the fileassert_test.log
for capture. Right now the optioninclude=FALSE
does not give any warning in the output, or the log.
Is this possible? If it isn't is it because I have fundamentally misunderstood how I should be approaching this goal?