json - Continuing a while loop after error in R -
i trying use while loop download bunch of json files.
they numbered 255 1. however, of them missing (for example, 238.json not exist).
scheduleurl <- "http://blah.blahblah.com/schedulejsonfile=" <- 255 while ( > 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) <- - 1 }
i want write while loop such if encounters nonexisting file (as when = 238), continues 237 instead of stopping.
i tried trycatch() function way, didn't work (keeps trying same url) :
while ( > 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) <- - 1} , error=function(e) e) if(inherits(possibleerror, "error")) { next } }
any appreciated!
url.exists rcurl package should trick.
library(rcurl) while ( > 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) } <- - 1 }
Comments
Post a Comment