如何在 Rmarkdown 编译时跳过错误检查?

How to skip error checking at Rmarkdown compiling?

我在 RStudio 中编写了一个 Rmarkdown 文档(编译为 HTML),并且有一些代码块故意产生错误。例如:

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

由于a没有之前的定义,这个块自然会产生类似object 'a' not found的错误信息。我希望在最终的 HTML 文件中显示此错误消息,但是当我在 RStudio 中按 Ctrl+Shift+K 到 "Knit HTML" 时,编译器报告错误并停止编织。

那么我如何告诉 knitr 在编译时忽略此类错误并将其显示在 knitted HTML 文档中?

使用error=TRUE: 来自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,RStudio 的 "Knit HTML" button/Ctrl-Shift-K 快捷方式背后的函数,默认设置 error=FALSE(对比 knitr::knit,默认设置为 error=TRUE)

```{r error=TRUE}
sum(a)
```