抑制Knitr中的错误消息 [英] Suppressing Error Messages in knitr

查看:102
本文介绍了抑制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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆