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

查看:11
本文介绍了抑制 knitr 中的错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何抑制 knitr 中的错误消息.我的 MWE 如下:

I wonder how to suppress error messages in knitr. My MWE is below:

documentclass{article} 
egin{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}
egin{document}

<<setup, include=FALSE, cache=FALSE>>=
muffleError <- function(x,options) {}
knit_hooks$set(error=muffleError)
@

<<Test>>=
1:10
X
@
end{document}

编译后会产生以下结果

这篇关于抑制 knitr 中的错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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