抑制Knitr中的错误消息 [英] Suppressing Error Messages in knitr
问题描述
我想知道如何抑制knitr
中的错误消息.我的MWE在下面:
I wonder how to suppress error messages in knitr
. My MWE is below:
\documentclass{article}
\begin{document}
<< Test >>=
1:10
X
@
\end{document}
已编辑
对象X
不存在.我想在代码块中显示X
并也要对其进行评估,即使这会引发错误.但是不想在我的.tex
文档中显示任何错误,就像我们可以通过设置warning=FALSE
来禁止显示警告一样.
The object X
does not exist. I want to show X
in my code chunk and want to evaluate it too even this will throw an error. But doesn't want to show any errors in my .tex
document same as we can suppress warnings by setting warning=FALSE
.
推荐答案
错误具有其专用的挂钩函数,存储在knit_hooks$get()
访问的环境中.为了给您带来启发,以下是这些功能的完整列表:
Errors have their own dedicated hook function, stored in the environment accessed by knit_hooks$get()
. Here, for your enlightenment, is the complete list of those functions:
names(knit_hooks$get())
# [1] "source" "output" "warning" "message" "error" "plot"
# [7] "inline" "chunk" "document"
要禁止显示警告,只需将默认错误挂钩函数改写为带有必需参数的函数,而根本不返回任何内容.
To suppress warnings, just overwrite the default error hook function with one that takes the required arguments, but doesn't return anything at all.
\documentclass{article}
\begin{document}
<<setup, include=FALSE, cache=FALSE>>=
muffleError <- function(x,options) {}
knit_hooks$set(error=muffleError)
@
<<Test>>=
1:10
X
@
\end{document}
经过编译后,将产生以下内容
Which, upon compilation, yields the following
这篇关于抑制Knitr中的错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!