Learn R Programming

mark (version 0.8.2)

handlers: Handlers

Description

Catch and report handlers

Usage

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)

Value

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

Arguments

x

A vector

FUN

A function

...

Additional params passed to FUN

.null

Logical, if FALSE will drop NULL results (for get_*())

Details

These functions can be used to catch whether an evaluation will return an error or warning without raising.

References

Function for catching has been adapted from https://stackoverflow.com/a/4952908/12126576

Examples

Run this code
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)

Run the code above in your browser using DataLab