Learn R Programming

SafeMapper (version 1.0.0)

s_possibly: Safe Possibly - Wrap Function to Return Default on Error

Description

Drop-in replacement for purrr::possibly that returns a default value when an error occurs instead of throwing the error.

Usage

s_possibly(.f, otherwise, quiet = TRUE)

Value

A function that returns the result or the default value.

Arguments

.f

A function to wrap for safe execution.

otherwise

Default return value when an error occurs.

quiet

Logical. Hide errors from console if TRUE.

Examples

Run this code
safe_log <- s_possibly(log, otherwise = NA)
safe_log(10) # Returns 2.30
safe_log("a") # Returns NA

Run the code above in your browser using DataLab