Learn R Programming

nprcgenekeepr (version 2.0.0)

safeExecute: Execute an expression with error handling

Description

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
)

Value

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

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.

See Also

logModuleEvent for logging

Examples

Run this code
# Returns 4
safeExecute({ 2 + 2 }, module = "test")

# Returns NULL and logs error
safeExecute({ stop("Error!") }, module = "test")

# Returns custom default on error
safeExecute({ stop("Error!") }, module = "test", default = data.frame())

Run the code above in your browser using DataLab