當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


R purrr insistently 將函數轉換為等待,然後在錯誤後重試


insistently() 接受一個函數並修改它,以便在出錯時在給定的時間後重試。

用法

insistently(f, rate = rate_backoff(), quiet = TRUE)

參數

f

要修改的函數,通過以下方式之一指定:

  • 命名函數,例如mean

  • 匿名函數,例如\(x) x + 1function(x) x + 1

  • 一個公式,例如~ .x + 1 。僅當您需要向後兼容舊版本的 R 時才推薦。

rate

一個rate 對象。默認為抖動指數退避。

quiet

隱藏錯誤(TRUE,默認值),還是在錯誤發生時顯示它們?

如上所述,該函數采用與 .f 相同的參數,但返回不同的值。

副詞

該函數稱為副詞,因為它修飾函數(動詞)的效果。如果您想在包中包含創建副詞的函數,請務必閱讀faq-adverbs-export

也可以看看

httr::RETRY() 是 HTTP 動詞 insistently() 的特例。

其他副詞:auto_browse() , compose() , negate() , partial() , possibly() , quietly() , safely() , slowly()

例子

# For the purpose of this example, we first create a custom rate
# object with a low waiting time between attempts:
rate <- rate_delay(0.1)

# insistently() makes a function repeatedly try to work
risky_runif <- function(lo = 0, hi = 1) {
  y <- runif(1, lo, hi)
  if(y < 0.9) {
    stop(y, " is too small")
  }
  y
}

# Let's now create an exponential backoff rate with a low waiting
# time between attempts:
rate <- rate_backoff(pause_base = 0.1, pause_min = 0.005, max_times = 4)

# Modify your function to run insistently.
insistent_risky_runif <- insistently(risky_runif, rate, quiet = FALSE)

set.seed(6) # Succeeding seed
insistent_risky_runif()
#> Error: 0.606268297648057 is too small
#> Retrying in 0.19 seconds.
#> Error: 0.264352067606524 is too small
#> Retrying in 0.15 seconds.
#> Error: 0.807483389042318 is too small
#> Retrying in 0.78 seconds.
#> [1] 0.9579337

set.seed(3) # Failing seed
try(insistent_risky_runif())
#> Error: 0.168041526339948 is too small
#> Retrying in 0.16 seconds.
#> Error: 0.384942351374775 is too small
#> Retrying in 0.13 seconds.
#> Error: 0.602100674761459 is too small
#> Retrying in 0.48 seconds.
#> Error: 0.124633444240317 is too small
#> Error in rate_sleep(rate, quiet = quiet) : 
#>   Request failed after 4 attempts.

# You can also use other types of rate settings, like a delay rate
# that waits for a fixed amount of time. Be aware that a delay rate
# has an infinite amount of attempts by default:
rate <- rate_delay(0.2, max_times = 3)
insistent_risky_runif <- insistently(risky_runif, rate = rate, quiet = FALSE)
try(insistent_risky_runif())
#> Error: 0.294600924244151 is too small
#> Retrying in 0.2 seconds.
#> Error: 0.577609919011593 is too small
#> Retrying in 0.2 seconds.
#> Error: 0.630979274399579 is too small
#> Error in rate_sleep(rate, quiet = quiet) : 
#>   Request failed after 3 attempts.

# insistently() and possibly() are a useful combination
rate <- rate_backoff(pause_base = 0.1, pause_min = 0.005)
possibly_insistent_risky_runif <- possibly(insistent_risky_runif, otherwise = -99)

set.seed(6)
possibly_insistent_risky_runif()
#> Error: 0.606268297648057 is too small
#> Retrying in 0.2 seconds.
#> [1] 0.937642

set.seed(3)
possibly_insistent_risky_runif()
#> Error: 0.168041526339948 is too small
#> Retrying in 0.2 seconds.
#> Error: 0.807516399072483 is too small
#> Retrying in 0.2 seconds.
#> Error: 0.384942351374775 is too small
#> [1] -99

相關用法


注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Transform a function to wait then retry after an error。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。