[Rd] Suppress specific warnings

Martin Morgan mtmorgan at fhcrc.org
Thu Apr 18 15:29:02 CEST 2013


On 04/18/2013 05:57 AM, Duncan Murdoch wrote:
> 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

maybe useful to make the point that it isn't hard to for the package you depend 
on to signal different types of warnings

   w = simpleWarning("curves ahead")
   class(w) = c("curves", class(w))
   warning(w)

more below...

> 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")
>     } )

...which are then caught as

   withCallingHandlers({
       warning(w)                   # 'curves'; caught
       warning("another warning")
   }, curves=function(w) invokeRestart("muffleWarning"))

The tricky part is to come up with an appropriate scheme for classifying warnings.

Martin Morgan

> Duncan Murdoch
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel


-- 
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109

Location: Arnold Building M1 B861
Phone: (206) 667-2793



More information about the R-devel mailing list