Downloading files
1. Downloading files
Similar to what we did in the previous video,2. Example: Excel
let's now use the readxl package to import Excel data stored at this URL. Will the read_excel work out of the box here? Apparently, readxl does not know how to handle Excel files that are stored on the web, although I expect that Hadley Wickham will add this functionality in future releases. Does this mean we'll have to manually download the file, and then point to the local file like you would do before? Not at all!3. download.file()
You can use download dot file here, a function from the utils package. Let's first define the URL and the destination path, so where we want to place the downloaded file on our system. I'm going to call the file local_cities dot xlsx and place it my home directory. If you now use download dot file with the url as the first argument and the destination path as the second argument you're good to go. Perfect. What download dot file did is simple. It performed a GET request to the URL you specified and stored the contents of the response to the location you specified. The last step is to use the read_excel function from before to import the Excel data from the local file this time. We can again use the dest_path variable here.4. Why download.file()?
You might wonder why using download dot file is useful. The answer is again: reproducibility. You can specify URLs in your R scripts and download them through R functions instead of manually browsing the web which requires several mouseclicks. There's much more to learn about performing GET requests and other HTTP requests from inside R. In some cases, you'll have to authenticate yourself before you can download files, and pass additional parameters to your GET request. If you want to learn more about this, I suggest you check out the httr package by Hadley Wickham.5. Let's practice!
Head over to the exercises for some more practice now, have fun!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.