I have a source file (in knitr) containing plots which use a particular font family. I'd like to suppress the warning messages
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")
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
.
Is there a way to suppress only the grid.Call
warning above throughout a file?
Use
withCallingHandlers({
<your code>
}, warning=function(w) {
if (<your warning>)
invokeRestart("muffleWarning")
})
For instance,
x = 1
withCallingHandlers({
warning("oops")
warning("my oops ", x)
x
}, warning=function(w) {
if (startsWith(conditionMessage(w), "my oops"))
invokeRestart("muffleWarning")
})
produces output
[1] 1
Warning message:
In withCallingHandlers({ : oops
>
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.
See Selective suppressWarnings() that filters by regular expression.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With