Skip to contents

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 eval_tidy().

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 as_data_mask() or new_data_mask(). Objects in data have priority over those in env. See the section about data masking.

env

The environment in which to evaluate expr. This environment is not applicable for quosures because they have their own environments.

Value

a named list

Examples

# function executes without error or warning
eval_capture_conditions(letters[1:2])
#> $result
#> [1] "a" "b"
#> 
#> $warning
#> NULL
#> 
#> $error
#> NULL
#> 

# an error is thrown
eval_capture_conditions(stop("Example Error!"))
#> $result
#> NULL
#> 
#> $warning
#> NULL
#> 
#> $error
#> [1] "Example Error!"
#> 

# if more than one warning is returned, all are saved
eval_capture_conditions({
  warning("Warning 1")
  warning("Warning 2")
  letters[1:2]
})
#> $result
#> [1] "a" "b"
#> 
#> $warning
#> [1] "Warning 1" "Warning 2"
#> 
#> $error
#> NULL
#> 

# messages are not printed to the console
eval_capture_conditions({
  message("A message!")
  letters[1:2]
})
#> $result
#> [1] "a" "b"
#> 
#> $warning
#> NULL
#> 
#> $error
#> NULL
#>