40

How can I simply tell R to retry a statement a few times if it errors? E.g. I was hoping to do something like:

tryCatch(dbGetQuery(...),           # Query database
    error = function(e) {
        if (is.locking.error(e))    # If database is momentarily locked
            retry(times = 3)        # retry dbGetQuery(...) 3 more times
        else {
            # Handle other errors
        }
    }
)

6 Answers 6

60

I usually put the try block in a loop, and exit the loop when it no longer fails or the maximum number of attempts is reached.

some_function_that_may_fail <- function() {
  if( runif(1) < .5 ) stop()
  return(1)
}

r <- NULL
attempt <- 1
while( is.null(r) && attempt <= 3 ) {
  attempt <- attempt + 1
  try(
    r <- some_function_that_may_fail()
  )
} 
2
  • 6
    Should set attempt=0 at start.
    – horaceT
    Commented Jan 26, 2018 at 17:47
  • @horaceT Why? With attempt <- 1 at the onset, there will be at most 3 attempts, which seems to be the aim. You could replace attempt <= 3 with attempt <= num_of_attempts. Commented Sep 26, 2022 at 17:20
18

I wrote a quick function that allows you to easily retry an operating a configurable number of times, with a configurable wait between attempts:

library(futile.logger)
library(utils)

retry <- function(expr, isError=function(x) "try-error" %in% class(x), maxErrors=5, sleep=0) {
  attempts = 0
  retval = try(eval(expr))
  while (isError(retval)) {
    attempts = attempts + 1
    if (attempts >= maxErrors) {
      msg = sprintf("retry: too many retries [[%s]]", capture.output(str(retval)))
      flog.fatal(msg)
      stop(msg)
    } else {
      msg = sprintf("retry: error in attempt %i/%i [[%s]]", attempts, maxErrors, 
                    capture.output(str(retval)))
      flog.error(msg)
      warning(msg)
    }
    if (sleep > 0) Sys.sleep(sleep)
    retval = try(eval(expr))
  }
  return(retval)
}

So you can just write val = retry(func_that_might_fail(param1, param2), maxErrors=10, sleep=2) to retry calling that function with those parameters, give up after 10 errors, and sleep 2 seconds between attempts.

Also, you can redefine the meaning of what an error looks like by passing a different function as parameter isError, which by default will catch an error signaled with stop. This is useful if the function being called does something else on error, such as returning FALSE or NULL.

This is the alternative I've found so far that results in clearer, more readable code.

Hope this helps.

16

A solution without pre-assigning values and using for instead of while:

some_function_that_may_fail <- function(i) {
  if( runif(1) < .5 ) stop()
  return(i)
}

for(i in 1:10){
  try({
    r <- some_function_that_may_fail(i)
    break #break/exit the for-loop
  }, silent = FALSE)
}

r will be equal to the number of tries that were needed. If you dont want the output of the errors set silent to TRUE

6

I have put together the code and make it package: retry

f <- function(x) {
    if (runif(1) < 0.9) {
        stop("random error")
    }
    x + 1
}

# keep retring when there is a random error
retry::retry(f(1), when = "random error")
#> [1] 2
# keep retring until a requirement is satisified.
retry::retry(f(1), until = function(val, cnd) val == 2)
#> [1] 2
# or using one sided formula
retry::retry(f(1), until = ~ . == 2, max_tries = 10)
#> [1] 2
1
  • Is it possible to incorporate a timeout for each attempt? For example, I have a function that should not take longer than 10 seconds to run. If it does take longer, I want this attempt to timeout, and then I want retry to try again. It looks like the timeout parameter within retry is limited to the global runtime, not each attempt. Commented Jun 8, 2022 at 18:06
5

Here's a function to generate a custom condition to respond to

locked <- function(message="occurred", ...) {
    cond <- simpleCondition(message, ...)
    class(cond) <- c("locked", class(cond))
    cond
}

and a function implemented to allow (an infinite number of) restarts

f <- function() {
    cnt <- 0L
    repeat {
        again <- FALSE
        cnt <- cnt + 1L
        withRestarts({
            ## do work here, and if needed...
            signalCondition(locked())
        }, retry=function() {
            again <<- TRUE
        })
        if (!again) break
    }
    cnt
}

and the use of withCallingHandlers (to keep the context where the condition was signaled active unlike tryCatch) to handle the locked condition

withCallingHandlers({
    n_tries <- 0L
    f()
}, locked=function(e) {
    n_tries <<- n_tries + 1L
    if (n_retries < 3)
        invokeRestart("retry")
})
2

I like setting my object as an error from the start, also sometimes useful to add some sleep time if you're having connection problems:

res <- simpleError("Fake error to start")
counter <- 1
max_tries <- 10 
# Sys.sleep(2*counter)
while(inherits(res, "error") & counter < max_tries) { 
  res <- tryCatch({ your_fun(...) }, 
      error = function(e) e)
  counter <- counter + 1
}
1
  • this needs to be & not OR :: while(inherits(res, "error") & counter < max_tries) { Commented Aug 19, 2019 at 11:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.