1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
#' Try evaluating an expressing multiple times until it succeeds.
#'
#' @param times Maximum number of attempts.
#' @param code Code to evaluate
#' @keywords internal
#' @export
#' @examples
#' third_try <- local({
#' i <- 3
#' function() {
#' i <<- i - 1
#' if (i > 0) fail(paste0("i is ", i))
#' }
#' })
#' try_again(3, third_try())
try_again <- function(times, code) {
while (times > 0) {
e <- tryCatch(
withCallingHandlers(
{
code
NULL
},
warning = function(e) {
if (identical(e$message, "restarting interrupted promise evaluation")) {
maybe_restart("muffleWarning")
}
}
),
expectation_failure = function(e) {
e
},
error = function(e) {
e
}
)
if (is.null(e)) {
return(invisible(TRUE))
}
times <- times - 1L
}
stop(e)
}
|