I am trying to use a while loop to download a bunch of JSON files.
They are numbered from 255 to 1. However, some of them are missing (for example, 238.json does not exist).
scheduleurl <- "http://blah.blahblah.com/schedulejsonfile="
i <- 255
while ( i > 0) {
last = paste0(as.character(i), ".json")
path = "/Users/User/Desktop/Temp"
fullpath = paste0(path, last)
ithscheduleurl <- paste0(scheduleurl, as.character(i))
download.file(ithscheduleurl, fullpath)
i <- i - 1
}
I basically want to write my while loop such that if it encounters a nonexisting file (as it will when i = 238), it basically continues to 237 instead of stopping.
I tried the tryCatch() function this way, but it didn't work (keeps trying the same URL) :
while ( i > 0) {
possibleError <- tryCatch({
last = paste0(as.character(i), ".json")
path = "/Users/dlopez/Desktop/Temp"
fullpath = paste0(path, last)
ithscheduleurl <- paste0(scheduleurl, as.character(i))
download.file(ithscheduleurl, fullpath)
i <- i - 1}
, error=function(e) e)
if(inherits(possibleError, "error")) {
next
}
}
Any help would be appreciated!
url.exists from the RCurl package should do the trick.
library(RCurl)
while ( i > 0) {
last = paste0(as.character(i), ".json")
path = "/Users/User/Desktop/Temp"
fullpath = paste0(path, last)
ithscheduleurl <- paste0(scheduleurl, as.character(i))
if (url.exists(ithscheduleurl)) {
download.file(ithscheduleurl, fullpath)
}
i <- i - 1
}