1

I have a code where I make requests for an API using the jsonlite package.

My request is:

aux <- fromJSON (www ... js)

The problem is that there is a time limit on requests and sometimes the error is returned:

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

I need that, when there is an error the code wait X seconds and make a new request and this is repeated until I get the requested data.

I found the try and tryCatch functions and the retry package. But I couldn't make it work as I need it.

1 Answers1

2

Try this approach :

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)})
}
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Thanks for the answer. Apparently it worked. This part of the code "error = function (e) {return (NA)}" only returns "NA" if TryCatch detects any error messages? Right? – Antonio Mendes Feb 10 '21 at 17:00
  • 1
    That is correct. If any error is detected `aux` will have value as `NA`. – Ronak Shah Feb 10 '21 at 23:48