error handling
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(message = FALSE)
stderr messages
stop()
- halts execution
logit <- function(x){
if( any(x < 0 | x > 1) ){
stop('x not between 0 and 1')
}
log(x / (1 - x) )
}
logit(0.4)
# error message without stop(): NaNs produced[1] NaN
# error message with stop(): Error in logit(-1) : x not between 0 and 1
warning()
- still allows function to execute,
- but throws a warning
# using ifelse() + warning
# output NA and warning
logit <- function(x){
x = ifelse( any(x < 0 | x > 1), NA, x )
if(any(is.na(x))){
warning('x not between 0 and 1')
}
log(x / (1 - x) )
}
logit(-1)
print(), paste(), cat(), message(), warning(), stop()
https://stackoverflow.com/questions/36699272/why-is-message-a-better-choice-than-print-in-r-for-writing-a-package
print()
, cat()
sends output to stdout
message()
, warning()
, stop()
sends output to stderr
print()
- cannot concatenate, prints [ x ] if multiple elements printed; but prints newline automatically
paste()
- allows concatenate
cat()
- allows concatenate, no [ x ]; but needs to specify newline manually
message()
- allows concat, no [ x ]; also needs to specify newline manually; but can be used with tryCatch()
warning()
- have problems when building packages, use message() instead
stop()
-
exceptions handling
try()
- allows execution to continue after error
f1 <- function(x) {
log(x)
return(c(log(x),10))
}
# f1("x")
# Error in log(x) : non-numeric argument to mathematical function
f1 <- function(x) {
t <- try({ # capture try() as a variable (if failure, returns an invisible "try-error" object)
a <- 1 # multiple expressions in try({})
b <- a + x
log(b)
}, silent = TRUE)
return(c(t,class(t),10)) # return try() results and outside results
}
f1("x")
f1(2)
tryCatch()
- take different actions depend on warnings,
- by mapping conditions to “handlers”
- built-in handlers = error, warning, message, interrupt
- catch-all handler = condition
- handlers return a value, or create a more informative error message
tryCatch(message("hello\n"), message=function(e){cat("goodbye\n")})
withCallingHandlers()
- …