This is a wrapper/helper 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()
.
Examples
f <- function(x) {x}
scoringutils:::run_safely(2, fun = f, metric_name = "f")
#> [1] 2
scoringutils:::run_safely(2, y = 3, fun = f, metric_name = "f")
#> [1] 2
scoringutils:::run_safely(fun = f, metric_name = "f")
#> Warning: ! Computation for `f` failed. Error: argument "x" is missing, with no default.
#> NULL
scoringutils:::run_safely(y = 3, fun = f, metric_name = "f")
#> Warning: ! Computation for `f` failed. Error: argument "x" is missing, with no default.
#> NULL