do_fun_wait: Safe execution of a function that might fail

Description Usage Arguments See Also Examples

Description

Safe execution of a function that might fail

Usage

1
do_fun_wait(.f, n_tries, sleep_for = 1L, ...)

Arguments

.f

A function

n_tries

Number of times to try executing the function

sleep_for

Number of seconds to make system sleep after each unsuccessful attempt

...

Arguments for .f

See Also

https://www.brodrigues.co/blog/2018-03-12-keep_trying/

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# Function success rate is `p`
get_data <- function(p = 0.8) {
  x <- rbinom(1, 1, p)
  ifelse(x == 0, "OK", stop("Error: too many calls!"))
  }

# Success on 3rd attempt
set.seed(556)
do_fun_wait(get_data, 10, 1)

# NULL if failed after n_tries
set.seed(55)
do_fun_wait(get_data, 10, 1)

# Pass .f arguments into ...
do_fun_wait(get_data, 10, 1, p = 0.6)

frankfarach/frankr documentation built on June 9, 2019, 11:40 a.m.