How to retry a statement on error?

后端 未结 6 1275
慢半拍i
慢半拍i 2020-12-01 06:19

How can I simply tell R to retry a statement a few times if it errors? E.g. I was hoping to do something like:

tryCatch(dbGetQuery(...),           # Query da         


        
6条回答
  •  生来不讨喜
    2020-12-01 06:59

    I have put together the code and make it package: retry

    f <- function(x) {
        if (runif(1) < 0.9) {
            stop("random error")
        }
        x + 1
    }
    
    # keep retring when there is a random error
    retry::retry(f(1), when = "random error")
    #> [1] 2
    # keep retring until a requirement is satisified.
    retry::retry(f(1), until = function(val, cnd) val == 2)
    #> [1] 2
    # or using one sided formula
    retry::retry(f(1), until = ~ . == 2, max_tries = 10)
    #> [1] 2
    

提交回复
热议问题