R/utl_try_try_try.R

Defines functions utl_try_try_try

Documented in utl_try_try_try

#' Try running a function and get a non-error response n times
#'
#' @param x A function.
#' @param n Number of times to try.
#' @export
#' @examples utl_try_try_try(a function)
utl_try_try_try <- function(x, n = 3L) {
  response <- "failed"
  attempt <- 1
  while (response == "failed" && attempt <= n) {
    print(sprintf("attempt: %s", attempt))
    attempt <- attempt + 1
    try({ response <- x }, silent = TRUE)
  }
  response
}
uva-bi-sdad/dc.utils documentation built on Aug. 1, 2022, 1:45 a.m.