inbo / fistools

Repo containing widely used data & functions of the FIS team
https://inbo.github.io/fistools/
Other
1 stars 0 forks source link

[NEW function] retry_function #82

Open SanderDevisscher opened 2 weeks ago

SanderDevisscher commented 2 weeks ago

Voorstel functie naam ""

Functionality

example code:

retry_function <- function(expr, max_attempts = 3) {
  attempts <- 0
  success <- FALSE
  result <- NULL

  while (attempts < max_attempts && !success) {
    attempts <- attempts + 1
    result <- tryCatch({
      eval(expr)  # Evaluate the expression
      success <- TRUE
      result  # Return the result if successful
    }, error = function(e) {
      message(sprintf("Attempt %d failed: %s", attempts, e$message))
      NULL  # Return NULL on error
    })
  }

  if (!success) {
    stop("All attempts failed.")
  }

  return(result)
}

Checklist

1in de mate van het mogelijke

2als je meer dan 5 functies en/of datasets in één pull request veranderd hebt gebruik: usethis::use_version(which = "major")