问题描述
我正在 RStudio 中编写 Rmarkdown 文档(编译为 HTML),并且有一些代码块故意生成错误.例如:
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)
```
因为之前没有对a
的定义,所以这个块自然会产生一个错误信息,比如object 'a' not found
.我希望在最终的 HTML 文件中显示此错误消息,但是当我在 RStudio 中按 Ctrl+Shift+K
以编织 HTML"时,编译器报告错误并停止编织.
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.
那么如何告诉 knitr
在编译时忽略此类错误并将其显示在编织的 HTML 文档中?
So how can I tell knitr
to ignore such error at compiling time and display it in the knitted HTML document?
推荐答案
Use error=TRUE
: from knitr 块选项的描述,
Use error=TRUE
: from the description of knitr chunk options,
error: (TRUE;logical) 是否保留错误(来自 stop());默认情况下,即使出现错误,评估也不会停止!!如果我们希望 R 在错误时停止,我们需要将此选项设置为 FALSE
rmarkdown::render
,RStudio 的Knit HTML"按钮/Ctrl-Shift-K 快捷键背后的函数,默认设置 error=FALSE
(与 knitr::knit
,默认为error=TRUE
)
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)
```
这篇关于如何在 Rmarkdown 编译时跳过错误检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!