R/retry.R

Defines functions retry

Documented in retry

#' Retry an expression
#'
#' @param foo The expression
#' @param tries Number of tries
#'
#' @return Output of expression or error
#' @export
#'
retry <- function(foo, tries = 3) {

  message(
    paste0(paste0("Trying expression... \nRemaining tries: ", tries, "\n")),
    appendLF = FALSE
  )

  withRestarts(
    tryCatch(
      { foo },
      error = function(e) { invokeRestart("restart") }
    ),
    restart = function() {
      message("Retrying...")
      stopifnot(tries > 0)
      retry(foo, tries-1)
    }
  )
}
schoulten/macroview documentation built on Jan. 4, 2023, 3:54 a.m.