Suppress any emission of a particular warning message

前端 未结 1 323
南方客
南方客 2021-01-18 01:05

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_textBo

相关标签:
1条回答
  • 2021-01-18 01:41

    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.

    0 讨论(0)
提交回复
热议问题