How to Use R to Download File from Internet (Example)
In this article, I’ll explain how to use the R programming language to download a file from the internet.
Let’s dive right in!
Step 1: Get URL of File
First, we need to copy the URL where our data is stored.
In this example, I’m going to use a csv file from this website: https://www.stats.govt.nz/large-datasets/csv-files-for-download/
On the website, you can find a list of downloadable csv files. Right click on one of them and copy the link location:
Figure 1: Get Link Location of csv File.
Now, store this link location as character string in R:
# Specify URL where file is stored url <- "https://www.stats.govt.nz/assets/Uploads/Annual-enterprise-survey/Annual-enterprise-survey-2017-financial-year-provisional/Download-data/annual-enterprise-survey-2017-financial-year-provisional-csv.csv"
Step 2: Set File Destination
In addition to the link location, we also need to specify where on our computer we want to save the data. The following R code illustrates how to define the path of the output directory:
# Specify destination where file should be saved destfile <- "C:/Users/ ... Your Path ... /my folder/output.csv"
Step 3: Download File with R
We are ready to download!
The base R function download.file enables us to download our file and save it in the specified directory. We simply need to tell the function the URL (Step 1) and the file destination (Step 2):
# Apply download.file function in R download.file(url, destfile)
Have a look at the folder that you have specified as file destination. You should find the downloaded data in csv format:
Figure 2: Downloaded csv File in Folder on Computer.
Note: R allows for the download of any file format you want. In the previous example, we have downloaded a csv file. However, you might also download Excel (xlsx / xls) files, txt files, zip files, PDF files and so on. Furthermore, it is possible to download files from a sharepoint or a web application such as shiny.
Video Example & Further Resources
Do you need further guidance for the downloading of files from the web? Then you could have a look at the following video of DevNami’s YouTube channel.
The video does not only show another example for the application of the download.file function. It also explains how to import this data to R (or RStudio).
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
In addition, you might also want to have a look at the other R tutorials on this website. I’m publishing new R and statistics tutorials regularly:
This article explained how to download data from the internet with the download.file R function. I case you have any further questions, let me know in the comments.
11 Comments. Leave new
With `read.csv` you can actually skip the step of downloading the file, if the URL points to the CSV file, as in the example above. Thus,
`read.csv(“https://www.stats.govt.nz/assets/Uploads/Annual-enterprise…provisional-csv.csv”)` will suffice.
Hey Victor,
Thanks a lot for this hint. Actually, I didn’t know that this is possible.
I think the code shown in this tutorial is still useful, because it creates a copy of the data on your computer. So it depends a bit on what you want to do with the data 🙂
Regards
Joachim
and when I have several files in a directory to download? how do I identify the .zip files available for download?
Hey Fernando,
Unfortunately, it seems like I have missed answering your question. Do you still have problems with this?
Regards,
Joachim
Good
Thank you Emmanuel 🙂
Hello dear Joachim ! very nice tutorial ! I wonder though if I could first check if the url and file exist and then download it.. I tried a loop with a series of files I want : paste0(“https://rest.uniprot.org/uniprotkb/”, accession,”.fasta”)) , where accession can be :P37108 , Q86VP3-3. But first exists, second doesn’t , so this stops my loop and throughs errror. How to capture error which an if verification, and move on to next ?
Hey Maritilia,
Thank you for the kind feedback, glad you like the tutorial.
Maybe you could check first whether a file exists using this method?
Regards,
Joachim
Thanks so much for the tutorial. Very useful.
If I have multiple links to download from, and I want all the contents in the links to come out as one single file. Please how do I go about it?
Hello Abiola,
You are very welcome. You can use the lapply() function with download.file() in the tutorial to download from multiple links. See the solution in StackOverflow.
Regards,
Cansu
Hayyy