【问题标题】:How can I delay the execution of a function within the purrr function如何在 purrr 函数中延迟函数的执行
【发布时间】:2018-03-22 10:44:51
【问题描述】:

使用purrr::map_dfr() 函数执行以下代码有时会限制每秒的 API 次数。

data.frame(location = c("sapporo", "aomori", "sendai", "morioka", "yamagata", "iwate")) %>%
  purrr::map_dfr(.f = function(x) ggmap::geocode(as.character(x)))

警告信息: 地理编码失败,状态为 OVER_QUERY_LIMIT,位置 =“青森”

我想输入sys.sleep (1) 之类的内容,但我该怎么做呢?

【问题讨论】:

  • 也许请参阅here,了解如何将 API 密钥与 ggmap 一起使用。这可能会完全解决您的问题。

标签: r purrr


【解决方案1】:

好问题!在函数调用系统内('sleep 10')

geoc <- function(x) {
gc <-ggmap::geocode(as.character(x))
system('sleep 5') # sleep 5 seconds
return(gc)
}
data.frame(location = c("sapporo", "aomori", "sendai", "morioka", "yamagata", 
"iwate")) %>%
 purrr::map_df(., geoc)

【讨论】:

    猜你喜欢
    • 2016-12-17
    • 2013-06-19
    • 1970-01-01
    • 1970-01-01
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多