[R] download.file

David Winsemius dwinsemius at comcast.net
Wed Nov 9 01:02:34 CET 2011


On Nov 8, 2011, at 6:32 PM, Mary Kindall wrote:

> I am downloading say 100 files from ucsc website and storing it into  
> dest
> folder.
> download.file function create a file in destination folder even if  
> the file
> is not present which is something I dont want.
> So I wrote if condition to remove the file if the download function  
> has non
> zero value.
>
> Now it exits when there is an error or file not present. How can I use
> "try" and "if" condition together so that the program does not exit on
> error and delete the created file in destination folder.
>
> for (i in 1: 100)
> {
> fileUrl = ucscfilenames[i]
> if (download.file(fileUrl, destFile, 'wget' , quiet = TRUE) != 0)
>    {
>    file.remove(destFile)
>    }
> }
>

I do not see where destFile gets its values. I don't think you can  
just give it a path.  And why would you want to remove a file that was  
never successfully created, anyway?

Wouldn't it be something like (pseudo-) :

<define a vector of destination file names>

if( try(download.file(fileUrl, destFile[i], 'wget' , quiet = TRUE) ) ) {
         <write to error log> }  else {
                      <write to success log> }


-- 

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list