run_safely: Run a function safely

View source: R/utils.R

run_safelyR Documentation

Run a function safely

Description

This is a wrapper function designed to run a function safely when it is not completely clear what arguments could be passed to the function.

All named arguments in ... that are not accepted by fun are removed. All unnamed arguments are passed on to the function. In case fun errors, the error will be converted to a warning and run_safely returns NULL.

run_safely can be useful when constructing functions to be used as metrics in score().

Usage

run_safely(..., fun)

Arguments

...

Arguments to pass to fun.

fun

A function to execute.

Value

The result of fun or NULL if fun errors

Examples

f <- function(x) {x}
run_safely(2, fun = f)
run_safely(2, y = 3, fun = f)
run_safely(fun = f)
run_safely(y = 3, fun = f)

epiforecasts/scoringutils documentation built on April 23, 2024, 4:56 p.m.