title: Skipping error files when downloading using download.file in R
body: <p>I have a larger number of links to pdf files that I would like to download using download.file in a for loop. My solution works fine, but it stops when it encounters an error (a number of the files does not work). I would like to add a feature to my download.file function that tells R to skip a file if downloaded yields an error and to print a message with the names of the pages for which an error was encountered. </p>
  
  <p>I found that tryCatch is likely a good solution in this case, but I am not entirely sure where to place it (I have tried a number of ways, but neither worked). </p>
  
  <p>Here is my code: </p>
  
  <pre><code>for (i in length(files) {
  
  # Reads the html links 
    html &lt;- read_html(files[i])
    reads_name &lt;- html_nodes(html, 'h1') 
    name &lt;- trimws(html_text(reads_name) )
  
  # Extracts the pdf. link from all links that the webpage contains 
    webpagelinks &lt;- html_attr(html_nodes(html, "a"), "href")
    extract_pdf_link &lt;- webpagelinks[grepl("\\pdf", webpagelinks)]
  
  # downloads the pdf file from the pdf link, here is where I get the error 
    download.file(extract_pdf_link, destfile = paste0(name, "_paper.pdf") , 
  mode = "wb")
  
    skip_with_message = simpleError('Did not work out')
    tryCatch(print(name), error = function(e) skip_with_message)
  
    }
  </code></pre>
  
  <p>Any suggestions on how to solve this? </p>
  
  <p>Many thanks!  </p>