retry: rerun a function call in case of error

Description Usage Arguments Details Value Source See Also 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,

...

ellipsis is to facilitate using retry within functions and retain control over verbosity

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 on the number 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.

Source

This function is lifted from package acutils.

See Also

dots

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/siscreenr documentation built on Nov. 26, 2021, 3:08 p.m.