[R] try / tryCatch for download.file( ) within a for loop when URL does not exist

Vishal Belsare shoot.spam at gmail.com
Tue Oct 16 23:39:05 CEST 2007


Jim,

Thanks. Actually I just got it working a few minutes ago with:

tryCatch({download.file(url, destfile = file, quiet = FALSE, mode =
"wb")}, silent = FALSE, condition = function(err) { } )

but I like your suggestion better. I'll attempt to log the list of
url's downloaded ok and those which encountered an error. Thanks!


Vishal

On 10/17/07, jim holtman <jholtman at gmail.com> wrote:
> I think this is closer to what you want. You can determine what you
> want to do; this one just goes to the next iteration:
>
>
> date <- as.POSIXlt(as.Date("2007-10-15", "%Y-%m-%d"))
> for (i in 1:difftime(as.POSIXlt(as.Date("2007-10-15",
> "%Y-%m-%d")),"2007-10-01"))
>        {
>        if (date$wday != 0 & date$wday != 6) {
>        datestr <- as.character(date, "%d%m%y") #make date character
> string ddmmyy
>        url <- paste("http://www.bseindia.com/bhavcopy/eq",datestr,"_csv.zip",sep="")
>        file <- paste("C:/",datestr,"_csv.zip",sep="")
>        #options(show.error.messages = FALSE)
>        #options(warn = -1)
>        err <- try(download.file(url, destfile = file, quiet = TRUE,
> mode = "wb"))
>        if (class(err) == "try-error") next  # skip this iteration on error
>        date <- as.POSIXlt(as.Date(date - 86400))
>        } else {date <- as.POSIXlt(as.Date(date - 86400))}
>        }
>
>
>
> On 10/16/07, Vishal Belsare <shoot.spam at gmail.com> wrote:
> > I am trying to download a bunch of files from a server, for which I am
> > using download.file( ) within a for loop. The script is working fine
> > except until download.file hits a URL which has no file, at which
> > point it exits. I want to change this behavior to simple log the
> > failure and maintain state within the for loop and iterate to next. I
> > read about try / tryCatch but am having trouble understanding what/how
> > it does. Thanks.
> >
> > #begin script
> >
> > date <- as.POSIXlt(as.Date("2007-10-15", "%Y-%m-%d"))
> > for (i in 1:difftime(as.POSIXlt(as.Date("2007-10-15",
> > "%Y-%m-%d")),"2007-10-01"))
> >        {
> >        if (date$wday != 0 & date$wday != 6) {
> >        datestr <- as.character(date, "%d%m%y") #make date character string ddmmyy
> >        url <- paste("http://www.bseindia.com/bhavcopy/eq",datestr,"_csv.zip",sep="")
> >        file <- paste("C:/",datestr,"_csv.zip",sep="")
> >        #options(show.error.messages = FALSE)
> >        #options(warn = -1)
> >        try(download.file(url, destfile = file, quiet = TRUE, mode = "wb"),
> > finally = cat("OK:",url,\n))
> >        date <- as.POSIXlt(as.Date(date - 86400))
> >        } else {date <- as.POSIXlt(as.Date(date - 86400))}
> >        }
> >
> > # end script
> >
> >
> > Thanks,
> >
> > Vishal Belsare
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > 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.
> >
>
>
> --
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
>
> What is the problem you are trying to solve?
>



More information about the R-help mailing list