R/call_user_fun.R

Defines functions call_user_fun

Documented in call_user_fun

#' Calls a Function Provided by the User
#'
#' Calls a function provided by the user and adds the function call to the error
#' message if the call fails.
#'
#' @param call Call to be executed
#'
#'
#' @return The return value of the function call
#'
#' @family utils_help
#' @keywords utils_help
#'
#' @export
#'
#' @examples
#' call_user_fun(compute_bmi(
#'   height = 172,
#'   weight = 60
#' ))
#'
#' try(call_user_fun(compute_bmi(
#'   height = 172,
#'   weight = "hallo"
#' )))
call_user_fun <- function(call) {
  tryCatch(
    eval_tidy(call),
    error = function(cnd) {
      abort(
        paste0("Calling ", as_label(enexpr(call)), " caused the following error:\n", cnd)
      )
    }
  )
}

Try the admiral package in your browser

Any scripts or data that you put into this service are public.

admiral documentation built on Oct. 19, 2023, 1:08 a.m.