Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip error checking at Rmarkdown compiling?

Tags:

r

r-markdown

I was writing an Rmarkdown document (compile to HTML) in RStudio, and there are some code chunks that deliberately generate errors. for example:

```{r} sum(a) ``` 

Since there is no previous definition for a this chunk will naturally generate an error message like object 'a' not found. I'd like this error message displayed in the final HTML file, but when I press Ctrl+Shift+K in RStudio to "Knit HTML", the compiler reported the error and stopped knitting.

So how can I tell knitr to ignore such error at compiling time and display it in the knitted HTML document?

like image 527
Benny Avatar asked Dec 15 '15 02:12

Benny


People also ask

How do I ignore errors in R Markdown?

In R Markdown, error = FALSE is the default, which means R should stop on error when running the code chunks.

How do I compile a R Markdown file?

The usual way to compile an R Markdown document is to click the Knit button as shown in Figure 2.1, and the corresponding keyboard shortcut is Ctrl + Shift + K ( Cmd + Shift + K on macOS). Under the hood, RStudio calls the function rmarkdown::render() to render the document in a new R session.

How do I hide code in R Markdown output?

Hide source code: ```{r, echo=FALSE} 1 + 1 ``` Hide text output (you can also use `results = FALSE`): ```{r, results='hide'} print("You will not see the text output.") ``` Hide messages: ```{r, message=FALSE} message("You will not see the message.") ``` Hide warning messages: ```{r, warning=FALSE} # this will generate ...

How do I stop R from running a chunk?

The shortcut to interrupt a running process in R depends on the R software and the operating system you are using. However, if you are using RStudio on a Windows computer, you can usually use Esc to stop a currently executing R script. Then, we can press Esc to interrupt the loop.


1 Answers

Use error=TRUE: from the description of knitr chunk options,

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::render, the function behind RStudio's "Knit HTML" button/Ctrl-Shift-K shortcut, sets error=FALSE by default (in contrast to knitr::knit, which defaults to error=TRUE)

```{r error=TRUE} sum(a) ``` 
like image 126
Ben Bolker Avatar answered Sep 30 '22 17:09

Ben Bolker