出错时如何发起新的请求? (来自 JSON)

How to make a new request while there is an error? (fromJSON)

我有一个代码,我使用 jsonlite 包请求 API。

我的要求是:

aux <- fromJSON (www ... js)

问题是请求有时间限制,有时会返回错误:

*Error in open.connection (con, "rb"): HTTP error 429.*

我需要它,当出现错误时,代码会等待 X 秒并发出新请求,并重复此过程,直到我获得请求的数据。

我找到了 trytryCatch 函数以及 retry 包。但是我无法让它按我的需要工作。

试试这个方法:

aux <- tryCatch(fromJSON (www ... js), error = function(e) {return(NA)})

while(all(is.na(aux))) {
  Sys.sleep(30) #Change as per requirement. 
  aux <- tryCatch(fromJSON(www ... js), error = function(e) {return(NA)})
}