Skip to content

Fetching and Saving warnings from a function

2 messages · Christofer Bogaso, William Dunlap

#
?withCallingHandlers

E.g.,
R> f <- function(expr) {
      warnings <- character()
      withCallingHandlers(expr, warning=function(e){
         warnings <<- c(warnings, conditionMessage(e))
         invokeRestart("muffleWarning")
      })
      warnings
}
R> f(1:10)
character(0)
R> f(warning("Hmm"))
[1] "Hmm"
R> f({warning("Hmm"); x <- 666 ; warning("Possible problem")})
[1] "Hmm"              "Possible problem"
R> x
[1] 666

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com