insistently()
接受一個函數並修改它,以便在出錯時在給定的時間後重試。
參數
- f
-
要修改的函數,通過以下方式之一指定:
-
命名函數,例如
mean
。 -
匿名函數,例如
\(x) x + 1
或function(x) x + 1
。 -
一個公式,例如
~ .x + 1
。僅當您需要向後兼容舊版本的 R 時才推薦。
-
- rate
-
一個rate 對象。默認為抖動指數退避。
- quiet
-
隱藏錯誤(
TRUE
,默認值),還是在錯誤發生時顯示它們?
副詞
該函數稱為副詞,因為它修飾函數(動詞)的效果。如果您想在包中包含創建副詞的函數,請務必閱讀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
相關用法
- R purrr imap 將函數應用於向量的每個元素及其索引
- R purrr accumulate 累積向量縮減的中間結果
- R purrr list_transpose 轉置列表
- R purrr as_vector 將列表強製轉換為向量
- R purrr map_if 有條件地將函數應用於向量的每個元素
- R purrr map2 映射兩個輸入
- R purrr array-coercion 強製數組列出
- R purrr auto_browse 包裝一個函數,以便在出錯時自動 browser()
- R purrr pluck 安全地獲取或設置嵌套數據結構深處的元素
- R purrr map_depth 在給定深度映射/修改元素
- R purrr list_simplify 將列表簡化為原子或 S3 向量
- R purrr rerun 多次重新運行表達式
- R purrr quietly 包裝一個函數來捕獲副作用
- R purrr list_flatten 壓平列表
- R purrr pmap 同時映射多個輸入(“並行”)
- R purrr possibly 包裝函數以返回值而不是錯誤
- R purrr head_while 查找全部滿足謂詞的頭/尾。
- R purrr rbernoulli 從伯努利分布生成隨機樣本
- R purrr rate-helpers 創建延遲率設置
- R purrr keep_at 根據元素的名稱/位置保留/丟棄元素
- R purrr keep 根據元素的值保留/丟棄元素
- R purrr transpose 轉置列表。
- R purrr flatten 將列表的列表展平為簡單的向量
- R purrr every 列表中的每個元素、部分元素還是沒有一個元素滿足謂詞?
- R purrr detect 查找第一個匹配的值或位置
注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Transform a function to wait then retry after an error。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。