Learn R Programming

catchr (version 0.2.31)

catch_expr: Catch conditions

Description

These are function that actually evaluate expression and "catch" the conditions. catch_expr() evaluates an expression, catching and handling the conditions it raises according to whatever catchr plans are specified. make_catch_fn() is a function factory that returns a function that behaves like catch_expr() with the plans already specified.

Plans can be passed in as output from make_plans() or as input that follows the same format as the input to make_plans().

Usage

catch_expr(expr, ..., .opts = NULL)

make_catch_fn(..., .opts = NULL)

Arguments

expr

the expression to be evaluated

a catchr plan as made by make_plans() or input for plans that follows the same format as input to make_plans()

.opts

The options to be used for the plans (generally passed in using catchr_opts()). If the input plans were already made by make_plans(), setting this will override whatever options were specified earlier.

Value

For catch_expr(): The value of the evaluated expression if there isn't an error and if the plans don't force an exit. If getOption("catchr.bare_if_possible") is FALSE (or if any conditions have been collect), it will return a named list, with the "value" element containing the value of the evaluated expression and sublists containing any collected conditions. For make_catch_fn() A function that catches conditions for expressions the same way catch_expr() would, but with the plans already specified.

Examples

Run this code
# NOT RUN {
warner <- function() {
  warning("Suppress this!")
  "done!"
}

compiled_warning_plans <- make_plans(warning = muffle)
warning_catcher <- make_catch_fn(warning = muffle)
warning_catcher2 <- make_catch_fn(compiled_warning_plans)

# `results` 1-4 are equivalent
results1 <- catch_expr(warner(), warning = muffle)
results2 <- warning_catcher(warner())
results3 <- catch_expr(warner(), compiled_warning_plans)
results4 <- warning_catcher2(warner())
# }

Run the code above in your browser using DataLab