Description Usage Arguments See Also Examples
Safe execution of a function that might fail
1 | do_fun_wait(.f, n_tries, sleep_for = 1L, ...)
|
.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 |
https://www.brodrigues.co/blog/2018-03-12-keep_trying/
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)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.