How to Download a File from the Internet using download.file()
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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:
# Specify the URL of the file you want to download url <- "https://example.com/data.csv" # Specify the file name and location where you want to save the file on your computer file_name <- "my_data.csv" file_path <- "/path/to/save/folder/" # 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")
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}
url <- "https://data.cms.gov/provider-data/sites/default/files/resources/49244993de5a948bcb0d69bf5cc778bd_1681445112/Measure_Dates.csv" file_name <- "measure_dates.csv" file_path <- "C:\\Downloads\\" 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.
measure_dates_df <- read.csv(file = paste0(file_path, file_name)) dplyr::glimpse(measure_dates_df)
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!
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.