如何延迟purrr函数

时间:2017-11-26 11:42:25

标签: r purrr

使用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,location =“aomori”

我想提出类似sys.sleep (1)的内容,但我该怎么做?

1 个答案:

答案 0 :(得分:0)

好问题!在函数调用系统中('睡眠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)