title: R:Check existence of "today's" file and if it doesn't exist, download it
body: <p>I'm trying to check to see if a user has an up-to-date file version of London's Covid prevalence data in their working directory and if not, download it from here:</p>
  <pre><code>fileURL &lt;-&quot;https://api.coronavirus.data.gov.uk/v1/data?filters=areaType=region;areaName=London&amp;structure=%7B%22areaType%22:%22areaType%22,%22areaName%22:%22areaName%22,%22areaCode%22:%22areaCode%22,%22date%22:%22date%22,%22newCasesBySpecimenDateRollingSum%22:%22newCasesBySpecimenDateRollingSum%22,%22newCasesBySpecimenDateRollingRate%22:%22newCasesBySpecimenDateRollingRate%22%7D&amp;format=csv&quot;
  </code></pre>
  <p>Pasting that URL into the browser downloads the csv file. using download.file(URL, &quot;data_.csv&quot;) creates junk. Why?</p>
  <p>So far I have:</p>
  <pre><code>library(data.table)
  
  #Look for COVID file starting with &quot;data_&quot;
  destfile &lt;- list.files(pattern = &quot;data_&quot;) 
  #Find file date
  fileDate&lt;-file.info(destfile)$ctime %&gt;%as.Date()
  
     if(!file.exists(destfile) | fileDate != today()){
       res &lt;- tryCatch(download.file(url = fileURL,
                                 destfile = paste0(&quot;data_&quot;,today(),&quot;.csv&quot;),
                                 method = &quot;auto&quot;),
                   error=function(e) 1)
       if(res!=1) COVIDdata&lt;-data.table::fread(destfile) #This doesn't read the file
     }
  </code></pre>
  <p>The function always downloads a file regardless of the date on it but it saves it an unreadable format. I've resorted to downloading the file every time as follows.</p>
  <pre><code>COVIDdata &lt;- data.table::fread(fileURL)
  </code></pre>
  <p>The junk file that gets downloaded is this:</p>
  <p><a href="https://i.stack.imgur.com/BBNHI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BBNHI.png" alt="enter image description here" /></a></p>