0

I wrote a script to download many PDF files from the internet.

> for (i in 1:length(links)) {
>     download.file(links[i], paste(fold, "/", i, ".pdf", sep=""), mode='wb')   }

However, when an error occurs in one of the downloads, the entire script stops. I would like the script to ignore the error and go to the next step. Is it possible?

Thanks

1 Answers1

0

You can embed the function download.file in a Try catch and handle the error. Here a basic tutorial.

Something like this:

f_download<-function(link,fold,i)
{
  tryCatch( {
 download.file(link, paste(fold, "/", i, ".pdf", sep=""), mode='wb')   
  },
 error = function (condition) {

   cat("ERROR\n")
 },
 finally =
 {})
}

The application:

fold<-"fold"
links<-c("test1","test2")

for (i in 1:length(links)) {

  f_download(links[i],fold,i)
}

In case of error, in screen:

ERROR
ERROR
Terru_theTerror
  • 4,918
  • 2
  • 20
  • 39