# Specify the URL of the file you want to download
<- "https://example.com/data.csv"
url
# Specify the file name and location where you want to save the file on your computer
<- "my_data.csv"
file_name <- "/path/to/save/folder/"
file_path
# Call the download.file() function, passing in the URL and file name/location as arguments
download.file(url, paste(file_path, file_name, sep = ""), mode = "wb")
Introduction
The download.file() function in R is used to download files from the internet and save them onto your computer. Here’s a simple explanation of how to use it:
- Specify the URL of the file you want to download.
- Specify the file name and the location where you want to save the file on your computer.
- Call the download.file() function, passing in the URL and file name/location as arguments.
Here’s an example:
In this example, we’re downloading a CSV file from “https://example.com/data.csv”, and saving it as “my_data.csv” in the “/path/to/save/folder/” directory on our computer.
The mode = “wb” argument specifies that we want to download the file in binary mode.
Once you run this code, the file will be downloaded from the URL and saved to your specified file location.
Let’s try a working example.
Example
We are going to download the Measure Dates file from the following location: {https://data.cms.gov/provider-data/dataset/4j6d-yzce}
<- "https://data.cms.gov/provider-data/sites/default/files/resources/49244993de5a948bcb0d69bf5cc778bd_1681445112/Measure_Dates.csv"
url
<- "measure_dates.csv"
file_name <- "C:\\Downloads\\"
file_path
download.file(url = url, destfile = paste0(file_path, file_name, sep = ""))
Now let’s read in the file in order to make sure it actually downloaded.
<- read.csv(file = paste0(file_path, file_name))
measure_dates_df
::glimpse(measure_dates_df) dplyr
Rows: 170
Columns: 6
$ Measure.ID <chr> "ASC_11", "ASC_12", "ASC_13", "ASC_14", "ASC_17"…
$ Measure.Name <chr> "Percentage of patients who had cataract surgery…
$ Measure.Start.Quarter <chr> "1Q2021", "1Q2019", "1Q2021", "1Q2021", "3Q2020"…
$ Start.Date <chr> "01/01/2021", "01/01/2019", "01/01/2021", "01/01…
$ Measure.End.Quarter <chr> "4Q2021", "4Q2021", "4Q2021", "4Q2021", "4Q2021"…
$ End.Date <chr> "12/31/2021", "12/31/2021", "12/31/2021", "12/31…
Voila!