3

I would like to use sys.sleep function when the request of an API I am using gives me an error back. Like this one:

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

Should I use trycatch?

data<- fromJSON("https://api.com")

Thanks !

ML_Enthousiast
  • 1,147
  • 1
  • 15
  • 39

1 Answers1

3

You can use try catch

tryCatch(myfunc(), error=function(e) Sys.sleep(1))
Carlos Santillan
  • 1,077
  • 7
  • 8
  • Thanks a lot ! It seems to work well, although it looks that it will just do Sys.sleep after 1 error, but after a 2nd error it won't do it again. So could I mix what you suggested with a while loop ? In order to say that "as long as there is an error, do Sys.sleep" – ML_Enthousiast May 31 '18 at 05:13
  • 2
    I think the following is what you are looking for. Take a look at the retry function that is posted , it will do to retry an operating a configurable number of times, with a configurable wait between attempts https://stackoverflow.com/questions/20770497/how-to-retry-a-statement-on-error – Carlos Santillan May 31 '18 at 19:39