In an attempt to generate code that runs without warnings and hence can be run with options(warn=2)
, I am looking for an implementation of the suppressWarnings
routine that would only filter warnings that match a given (vector of) regular expressions. Some warnings are just beyond my control, like the famous
Unrecognized record type 7, subtype 18 encountered in system file
when reading certain SPSS files, and I want to selectively suppress these without affecting possible other warnings.
Is there already an implementation of this functionality?
I wrote a wrapper around @martin-morgan's answer for convenience, it works like
SuppressWarnings
except that you can pass a regular expression to the second argument (which will be passed togrepl
) or a function that will be applied to the error message, using the dots as additional arguments.I made it support the formula notation.
See examples below.
Suppress warnings with
withCallingHandlers
andinvokeRestart
, using the "muffleWarning" restart mentioned on?warning
This has the output
(use
tryCatch
if instead you would like to stop on warning). As @BenBolker mentions this doesn't handle translations; making a more elaborate regex isn't going to be satisfactory. For catching one's own warnings, one could make and throw a subclass of warning.