I am downloading data from google trends. Sometimes the connection drops and I have to restart the loop from the beginning (1-726). Suppose error is on 721th attempt still, I have to restart it from 1 again.
library(jsonlite)
baseurl <- "https://www.googleapis.com/trends/v1beta/graph?&terms="
for(i in 1:726){
mydata <- fromJSON(paste0(baseurl, file.list$query[i]), flatten=TRUE)
message("Retrieving query ", i)
pages[[i]] <- mydata$lines
}
The error looklikes this
Retrieving query 1
Retrieving query 2
Retrieving query 3
Error in open.connection(con, "rb") : HTTP error 503.
Then I have to restart the whole process repeatedly to get the desired data. Can there be a method that it retries itself without losing the progress?