eval_capture_conditions {cards} | R Documentation |
Evaluate and Capture Conditions
Description
Evaluates an expression while also capturing error and warning conditions.
Function always returns a named list list(result=, warning=, error=)
.
If there are no errors or warnings, those elements will be NULL
.
If there is an error, the result element will be NULL
.
Messages are neither saved nor printed to the console.
Evaluation is done via rlang::eval_tidy()
. If errors and warnings are produced
using the {cli}
package, the messages are processed with cli::ansi_strip()
to remove styling from the message.
Usage
eval_capture_conditions(expr, data = NULL, env = caller_env())
Arguments
expr |
An expression or quosure to evaluate. |
data |
A data frame, or named list or vector. Alternatively, a
data mask created with |
env |
The environment in which to evaluate |
Value
a named list
Examples
# function executes without error or warning
eval_capture_conditions(letters[1:2])
# an error is thrown
eval_capture_conditions(stop("Example Error!"))
# if more than one warning is returned, all are saved
eval_capture_conditions({
warning("Warning 1")
warning("Warning 2")
letters[1:2]
})
# messages are not printed to the console
eval_capture_conditions({
message("A message!")
letters[1:2]
})