[R] How to catch both warnings and errors?

Samuel Meyer sam at mcneilco.com
Wed Jan 9 22:13:18 CET 2013


I tried to use this solution (from over two years ago, but it remains an official demo), but found that it only captured the last warning rather than all of them. Instead, the code below collects all warnings.

tryCatch.W.E <- function(expr)
{
    W <- list()
    w.handler <- function(w){ # warning handler
	W <<- c(W,list(w))
	invokeRestart("muffleWarning")
    }
    list(value = withCallingHandlers(tryCatch(expr, error = function(e) e),
				     warning = w.handler),
	 warning = W)
}
Sam Meyer




More information about the R-help mailing list