Suppose I have the following RMarkdown
document:
warnings.Rmd
---
title: "Warnings"
output: html_document
---
```{r include = FALSE, warning = FALSE}
warning("warning 1")
```
```{r include = FALSE, warning = TRUE}
warning("warning 2")
```
```{r include = TRUE, warning = FALSE}
warning("warning 3")
```
```{r include = TRUE, warning = TRUE}
warning("warning 4")
```
All I want is that the warning does not appear in the document (basically warning = FALSE
), but I want to see it on the console while rendering the report, That is, in the output generated by
rmarkdown::render("warnings.Rmd")
I would like to see somewhere a line giving me the warning. The use-case for this is that I want the user to generate reports with some parameters. If some of those are out of bounds, I can set it to a default value, but I want to inform the user that this was done. I could print the warning to the document, but there it might be overlooked. Any ideas?
This is a bit of a hack, but perhaps you can override base::warning
outside the call to render()
:
warnings <- list()
warning <- function(...) warnings <<- c(warnings, list(...))
rmarkdown::render("quux.Rmd")
# processing file: quux.Rmd
# 1/8
# 2/8 [unnamed-chunk-1]
# 3/8
# 4/8 [unnamed-chunk-2]
# 5/8
# 6/8 [unnamed-chunk-3]
# 7/8
# 8/8 [unnamed-chunk-4]
# output file: quux.knit.md
# ...
# Output created: quux.html
warnings
# [[1]]
# [1] "warning 1"
# [[2]]
# [1] "warning 2"
# [[3]]
# [1] "warning 3"
# [[4]]
# [1] "warning 4"
Other than this, I think the canonical ways in R do not work as one would hope:
knitr
does not export any way to fine-tune control signals (errors, warnings), only the warning=T/F
chunk option that you are setting
knitr
installs its own call-handlers, so we cannot use globalCallingHandlers()
either in a chunk or globally around the entire render()
(and the author said as much)
that last comment suggests litedown
supports warning=NA
for
No special handling (warnings will be sent to the R console by default)
but in testing it does not do what you are trying to do.
I should note that my warnings <<- ...
trick is unlikely to work where the environment is not shared, such as when rendered using quarto
.