禁止发出任何特定警告消息 [英] Suppress any emission of a particular warning message
问题描述
我有一个源文件(在 knitr 中),其中包含使用特定字体系列的绘图.我想取消警告信息
I have a source file (in knitr) containing plots which use a particular font family. I'd like to suppress the warning messages
在 grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... : font在 Windows 字体数据库中找不到家族
In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... : font family not found in Windows font database
library(ggplot2)
ggplot(mtcars, aes(mpg, cyl, label = gear)) +
geom_text(family = "helvet")
我知道我可以在脚本 options(warn = -1)
中抑制所有警告消息,并且我知道如何使用 suppressWarnings
.我还可以在 tryCatch
中包围一个特定的块.
I know I can suppress all warning messages in a script options(warn = -1)
, and I know how to use suppressWarnings
. I can also surround a particular chunk in a tryCatch
.
有没有办法只抑制整个文件上方的grid.Call
警告?
Is there a way to suppress only the grid.Call
warning above throughout a file?
推荐答案
使用
withCallingHandlers({
<your code>
}, warning=function(w) {
if (<your warning>)
invokeRestart("muffleWarning")
})
例如
x = 1
withCallingHandlers({
warning("oops")
warning("my oops ", x)
x
}, warning=function(w) {
if (startsWith(conditionMessage(w), "my oops"))
invokeRestart("muffleWarning")
})
产生输出
[1] 1
Warning message:
In withCallingHandlers({ : oops
>
限制是 conditionMessage 可能会被翻译成另一种语言(特别是如果来自基本函数),因此文本将无法可靠识别.
The limitation is that the conditionMessage may be translated to another language (especially if from a base function) so that the text will not be reliably identified.
这篇关于禁止发出任何特定警告消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!