当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。