[Rd] Suppress specific warnings

Duncan Murdoch murdoch.duncan at gmail.com
Thu Apr 18 14:57:12 CEST 2013


On 13-04-18 7:31 AM, Felix Schönbrodt wrote:
> Hello,
>
> is there a convenient way to suppress only *specific* warnings? (I know about ?suppressWarnings)
> I depend on another package, from which I want to suppress only some warnings, but not others.

This is difficult in most cases, because most packages don't give enough 
information to distinguish among their warnings.  You could probably do 
it based on the message as follows, but the test won't work if someone 
is running in a locale where the warning message has been translated to 
a different language:

x <- 1:10
x + 1:3   # gives the warning:

In x + 1:3 :
   longer object length is not a multiple of shorter object length

To suppress just this one, try this:

withCallingHandlers(x + 1:3,
    warning=function(w) {
       if (grepl("longer object length", w$message))
          invokeRestart("muffleWarning")
    } )

Duncan Murdoch



More information about the R-devel mailing list