download.file not cleaning up inside tryCatch

I am using download.file to grab files from a website. I am looping over year since year is in the name of the file i am trying to grab. For some years there are no data and therefore no file. This throws an error in download.file and the file created by download.file is automatically removed. Because of this error i have it wrapped inside a tryCatch function to handle the error and continue for years where there are data. eg

 result <- tryCatch(
  res <- TRUE
  error = function(e) return(FALSE),
  warning = function(w) return(FALSE)
 if (!result) {

However download.file creates an empty file in destfile. (Which it usually automatically removes when allowed to error and terminate). If i try remove it from within R using file.remove or unlink i get a warning saying:

Warning message: In file.remove(destfile) : cannot remove file '.../2000.txt.gz', reason 'Permission denied'

If i try to remove this file in windows explorer i get " The action can't be completed because the file is open in Rstudio R session".

It seems like catching the error in the the tryCatch function prevents download.file from cleaning up properly. Has anyone experienced this or know how to rectify this?

Am i missing something simple?

I am on windows 10 using Rstudio 1.2.1135 and R 3.6.1


I dont completely understand this issue, but I may have a solution for you...
there a package called 'downloader'
I've tested the following , perhaps try it in your case ?

fpath <- ""
destfile <- file.path(getwd(),"testfilefail.txt")
silent_download <- function ( fpath,destfile){
result <- tryCatch(
    res <- TRUE
  error = function(e) {
    print("dodge error") 
  warning = function(w) {
    print("dodge warning") 
1 Like

I'm not able to recreate this problem. Can you give the steps for us to see it? For example, what's fpath in your example? Do you have the file open in another tab of R Studio?

@nirgrahamuk Brilliant! Thanks!. Strange since downloader::download is just a wrapper for download.file. Must be a fix in there for this exact issue, since when it is run, you see the temporary file being created, then immediately removed upon the error being caught. If you do the same thing for download.file the file is not removed.

@nwerth If you use @nirgrahamuk 's code and substitute download.file for downloader you'll see the difference.

This could be a windows only issue

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.