Skip to contents

Executes an expression with comprehensive error handling. On error, logs the error and returns a default value instead of stopping execution. This is particularly useful in Shiny reactive contexts where errors should be handled gracefully.

Usage

safeExecute(
  expr,
  module = "unknown",
  default = NULL,
  silent = FALSE,
  notify = FALSE
)

Arguments

expr

An expression to evaluate.

module

character. Name of the calling module for logging purposes.

default

The value to return if an error occurs. Defaults to NULL.

silent

logical. If TRUE, suppresses the error and warning logging. Defaults to FALSE.

notify

logical. If TRUE and in a Shiny context, shows a notification to the user. Defaults to FALSE.

Value

The result of evaluating expr, or default if an error occurs.

See also

logModuleEvent for logging

Examples

# Returns 4
safeExecute({ 2 + 2 }, module = "test")
#> [1] 4

# Returns NULL and logs error
safeExecute({ stop("Error!") }, module = "test")
#> [2026-07-15 14:49:41] [ERROR] [test] Error: Error!
#> NULL

# Returns custom default on error
safeExecute({ stop("Error!") }, module = "test", default = data.frame())
#> [2026-07-15 14:49:41] [ERROR] [test] Error: Error!
#> data frame with 0 columns and 0 rows