From bbolker at gmail.com  Sun Oct 21 21:28:03 2012
From: bbolker at gmail.com (Ben Bolker)
Date: Sun, 21 Oct 2012 15:28:03 -0400
Subject: [Rd] suppress *specific* warnings?
Message-ID: <50844CC3.6020302@ufl.edu>


  Not desperately important, but nice to have and possibly of use to
others, is the ability to suppress specific warnings rather than
suppressing warnings indiscriminately.  I often know of a specific
warning that I want to ignore (because I know that's it's a false
positive/ignorable), but the current design of suppressWarnings() forces
me to ignore *any* warnings coming from the expression.

  I started to write a new version that would check and, if supplied
with a regular expression, would only block matching warnings and
otherwise would produce the warnings as usual, but I don't quite know
enough about what I'm doing: see ??? in expression below.

  Can anyone help, or suggest pointers to relevant
examples/documentation (I've looked at demo(error.catching), which isn't
helping me ... ?)

suppressWarnings2 <- function(expr,regex=NULL) {
    opts <- options(warn = -1)
    on.exit(options(opts))
    withCallingHandlers(expr, warning = function(w) {
        ## browser()
        if (is.null(regex) || grepl(w[["message"]],regex)) {
            invokeRestart("muffleWarning")
        } else {
            ## ? what do I here to get the warning issued?
            ## browser()
            ## computeRestarts() shows "browser",
            ##    "muffleWarning", and "abort" ...
            options(opts)
            warning(w$message)
            ## how can I get back from here to the calling point
            ##   *without* muffling warnings ... ?
        }
    })
}

suppressWarnings2(sqrt(-1))
suppressWarnings2(sqrt(-1),"abc")

  It seems to me I'd like to have a restart option that just returns to
the point where the warning was caught, *without* muffling warnings ...
?  But I don't quite understand how to set one up ...

  Ben Bolker



[Rd] suppress *specific* warnings?
Ben Bolker