RDJ
RDJ

Reputation: 4122

R Markdown - How to prevent Knitr from repeatedly downloading a file?

When working on an R Markdown Rmd., can I prevent Knitr from downloading a file each time the Markdown is knitted?

My code chunk is:

download.file(url = paste('https://d396qusza40orc.cloudfront.net/',
                      'repdata/data/StormData.csv.bz2',
                      sep = ''),
          destfile = './storm.csv.bz2',
          method = 'curl')) 

The system time of the chunk isn't that significant in and by itself:

user    system   elapsed 
0.893   1.139    28.825 

But perhaps there's a way to cache the download or something so I can review the HTML quicker.

Upvotes: 1

Views: 695

Answers (2)

hrbrmstr
hrbrmstr

Reputation: 78792

Use httr, GET and write_disk since, if destfile exists, write_disk will not let GET perform the download (acts like a mini-cache operation). GET also uses RCurl under the covers.

library(httr)

try(GET(url, write_disk(destfile)))

Upvotes: 3

Elin
Elin

Reputation: 6755

You need to check if the file exists before attempting to download.

   destfile <- './storm.csv.bz2'
    if (!file.exists(destfile))
    {
      your code
    }

Upvotes: 5

Related Questions