Last active
March 1, 2016 11:45
-
-
Save jilmun/fa8438cecbcb32254388 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# custom tryCatch to return result and warnings -- http://stackoverflow.com/a/24569739/2271856 | |
myTryCatch <- function(expr) { | |
warn <- err <- NULL | |
value <- withCallingHandlers( | |
tryCatch(expr, error=function(e) { | |
err <<- e | |
NULL | |
}), warning=function(w) { | |
warn <<- w | |
invokeRestart("muffleWarning") | |
}) | |
list(value=value, warning=warn, error=err) | |
} | |
# example use cases | |
myTryCatch(1+1) # legit | |
myTryCatch(log(-1)) # NaN + warning | |
myTryCatch("a"+1) # error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment