[R] Skip error in downloading file in loop

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Sat Oct 21 18:50:16 CEST 2017


One of the recommendations in the Posting Guide is to read the archives before posting... even following along with recent posts would have pointed out the existence of the tryCatch function. 

?tryCatch
-- 
Sent from my phone. Please excuse my brevity.

On October 21, 2017 8:01:02 AM PDT, Miluji Sb <milujisb at gmail.com> wrote:
>I am trying to download data from NASA web-service.
>
>I am using the following code;
>
>for( i in 1:8) {
>  target1 <- paste0("
>https://hydro1.gesdisc.eosdis.nasa.gov/daac-bin/access/timeseries.cgi?variable=NLDAS:NLDAS_FORA0125_H.002:TMP2m&location=GEOM:POINT(
>",
>                    cities[i, "lon_nldas"],
>                    ",%20", cities[i,"lat_nldas"],
>
>")&startDate=1980-01-01T00&endDate=2016-12-31T23&type=asc2")
>  target2 <- paste0("/Users/dasgupta/Dropbox (FEEM)/Flu Paper/climate
>data/temperature/nldas/",    # change for whatever destination
>directory
>you may prefer.
>                    cities[i,"city"], "_",
>                    cities[i,"state"], ".csv")
>  download.file(url=target1, destfile=target2,  method = "libcurl")
>}
>
>Any time the coordinates provided is out data coverage, the loop fails
>and
>stops. Is there any way to force R to skip the error and continue with
>the
>rest of the download?
>
>My data looks like this:
>
>dput(droplevels(head(cities, 8)))
>structure(list(city = structure(1:8, .Label = c("Boston", "Bridgeport",
>"Cambridge", "Fall River", "Hartford", "Lowell", "Lynn", "New Bedford"
>), class = "factor"), state = structure(c(2L, 1L, 2L, 2L, 1L,
>2L, 2L, 2L), .Label = c(" CT ", " MA "), class = "factor"), lon_nldas =
>c(-71.05673836,
>-73.19126922, -71.1060061, -71.14364822, -72.67401574, -71.31283992,
>-70.82521832, -70.80586599), lat_nldas = c(42.35866236, 41.18188268,
>42.36679363, 41.69735342, 41.76349276, 42.64588413, 42.46370197,
>41.63767375)), .Names = c("city", "state", "lon_nldas", "lat_nldas"
>), row.names = c(NA, 8L), class = "data.frame")
>
>Any help will be appreciated. Thank you very much!
>
>Sincerely,
>
>Milu
>
>	[[alternative HTML version deleted]]
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list