rerror-handlingr-markdownknitrsweave

why suppress Error in Rmarkdown doesn't work?


I want to supress Errors in Rmw files. So, i've tried to set the global chunk option error=TRUE, but it doesn't work. Also it doesn't work to set the chunk option error=TRUE directly in the chunk. Here is an example code:


\begin{document}
\SweaveOpts{concordance=TRUE}

abc 
<<setup, cache=F, include=F>>=
library(knitr)
library(formatR)
opts_chunk$set(error=TRUE)
knit_hooks$set(error=TRUE)
@

<<a,error=TRUE>>=
A <- 5
# of course, that doesnt work, but i want the error message as chunk output
A * B
@
\end{document}

I don't understand why it doesn't work

Only the error message: "Error in eval(expr, .GlobalEnv) : object 'L' not found" appears.


Solution

  • You seem to be using Sweave from base R rather than knitr. If you were using knitr, you'd get a warning about the \SweaveOpts{concordance=TRUE} statement.

    If you are using RStudio, this is one of the Project Options. If you are running things directly, run knitr::knit("<your filename>"), instead of Sweave("<your filename>").

    There are a couple of other errors that will stop knitr from working; this version fixes them:

    \documentclass{article}
    
    \begin{document}
    
    abc 
    <<setup, cache=F, include=F>>=
    library(knitr)
    library(formatR)
    opts_chunk$set(error=TRUE)
    @
    
    <<a,error=TRUE>>=
    A <- 5
    # of course, that doesnt work, but i want the error message as chunk output
    A * B
    @
    \end{document}
    

    The changes were: