retry: rerun a function call in case of error

Description Usage Arguments Details Value Examples

View source: R/fun-retry.R

Description

If a call returns an error, run it again.

Usage

1
retry(call, max.iter, fail = NA, verbose = FALSE)

Arguments

call

call to retry

max.iter

maximum number of iterations after which failure is declared

fail

object to return in case of failure

verbose

logical flag whether to print iterations as messages

Details

Iterative calling of a function that can return a random error can crash a process or at the very least result in incomplete data. To alleviate the impact of random errors the failed call will be rerun until it succeeds or until a limit of attempts is reached. If all attempts fail, a backup value will be returned.

Value

retry returns the same as the function in call or as defined by fail.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
f <- function() {
  x <- sample(1:2, size = 1)
  if (x == 1) stop("it's a 1", call. = TRUE)
  return(x)
}
## Not run: 
f()
replicate(10, f())

## End(Not run)

retry(f(), 2, "failed", TRUE)
table(replicate(100, retry(f(), 2, "failed", FALSE)))

olobiolo/acutils documentation built on Nov. 28, 2021, 9:35 p.m.