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(
{
download.file(fpath,destfile=destfile,quiet=TRUE)
res <- TRUE
},
error = function(e) return(FALSE),
warning = function(w) return(FALSE)
)
if (!result) {
file.remove(destfile)
next
}
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
Thanks