handlers | R Documentation |
Catch and report handlers
has_warning(x, FUN, ...)
has_error(x, FUN, ...)
has_message(x, FUN, ...)
get_warning(x, FUN, ..., .null = TRUE)
get_message(x, FUN, ..., .null = TRUE)
get_error(x, FUN, ..., .null = TRUE)
x |
A vector |
FUN |
A function |
... |
Additional params passed to |
.null |
Logical, if |
These functions can be used to catch whether an evaluation will return an error or warning without raising.
The has_*()
functions will return TRUE
/FALSE
for if the handler
is found in the execution of the code. The get_*()
functions provide the
text of the message
Function for catching has been adapted from https://stackoverflow.com/a/4952908/12126576
has_warning(c(1, "no"), as.integer)
# 1 no
# FALSE TRUE
get_warning(c(1, "no"), as.integer)
# drop NULLs
get_warning(c(1, "no"), as.integer, .null = FALSE)
foo <- function(x) {
stopifnot(x > 0)
x
}
has_error(c(1, 0, 2), foo)
# 1 0 2
# FALSE TRUE FALSE
get_error(c(1, 0, 2), foo)
# drop NULLs
get_error(c(1, 0, 2), foo, .null = FALSE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.