How to retry a statement on error?

后端 未结 6 1277
慢半拍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:44

    I wrote a quick function that allows you to easily retry an operating a configurable number of times, with a configurable wait between attempts:

    library(futile.logger)
    library(utils)
    
    retry <- function(expr, isError=function(x) "try-error" %in% class(x), maxErrors=5, sleep=0) {
      attempts = 0
      retval = try(eval(expr))
      while (isError(retval)) {
        attempts = attempts + 1
        if (attempts >= maxErrors) {
          msg = sprintf("retry: too many retries [[%s]]", capture.output(str(retval)))
          flog.fatal(msg)
          stop(msg)
        } else {
          msg = sprintf("retry: error in attempt %i/%i [[%s]]", attempts, maxErrors, 
                        capture.output(str(retval)))
          flog.error(msg)
          warning(msg)
        }
        if (sleep > 0) Sys.sleep(sleep)
        retval = try(eval(expr))
      }
      return(retval)
    }
    

    So you can just write val = retry(func_that_might_fail(param1, param2), maxErrors=10, sleep=2) to retry calling that function with those parameters, give up after 10 errors, and sleep 2 seconds between attempts.

    Also, you can redefine the meaning of what an error looks like by passing a different function as parameter isError, which by default will catch an error signaled with stop. This is useful if the function being called does something else on error, such as returning FALSE or NULL.

    This is the alternative I've found so far that results in clearer, more readable code.

    Hope this helps.

提交回复
热议问题