mixtrak mixtrak - 3 months ago 12
R Question

Include errors in R markdown package vignette

I'm developing an R package and have a custom function which contains a

if(condition) stop("Error message")
conditional. I call this function in a package vignette with the intention of generating the error message and including that in the vignette. However, this is causing vignette building to fail.

How can I force vignette building to proceed even when the code generates error messages, and retain those error messages in the vignette document?

Answer

The knitr chunk options documentation says:

error: (TRUE; logical) whether to preserve errors (from stop()); by default, the evaluation will not stop even in case of errors!! if we want R to stop on errors, we need to set this option to FALSE

rmarkdown's render() function resets this to be FALSE by default (unlike knitr itself), arguably a better default. You can override this and set it back to TRUE by (I think) either

  • setting error=TRUE in the options for a particular chunk, or
  • using knitr::opts_chunk$set(error=TRUE) in an early code chunk to set the option globally.

I would suggest the former (i.e., only allow errors where you are expecting them ...)

Comments