Table of Contents
To download files from the internet using R, follow these steps:
1. Install the “download.file” package by typing “install.packages(“download.file”)” in the R console.
2. Load the package by typing “library(download.file)”.
3. Use the “download.file” function to specify the URL of the file you want to download and the destination path where you want to save it. For example, “download.file(url = “https://www.example.com/file.txt”, destfile = “C:/Downloads/file.txt”)”.
4. If the download requires authentication, use the “username” and “password” arguments in the “download.file” function.
5. To check if the download was successful, use the “file.exists” function to see if the file was saved to the specified destination path.
By following these steps, you can easily download files from the internet using R.
Download Files from the Internet Using R
You can use the following basic syntax to download a file from the internet using the R programming language:
download.file(url, destfile)where:
- url: A character string that contains the URL of the file
- destfile: A character string that contains the location of where to save the file
The following step-by-step example shows how to use this syntax in practice.
Step 1: Find URL of File
For this example, I’ll download a CSV file that contains information about model aircraft fields in New York located at the following URL:
To get the exact URL for this CSV file, I’ll right click on the CSV button and then click Copy link address:

I’ll then save this URL as a string variable in R:
#define URL location
url<-"https://data.cityofnewyork.us/api/views/brsj-szf5/rows.csv?accessType=DOWNLOAD"
Step 2: Define Destination for File
Next, I’ll define the destination to save the file to:
#define destination for file
destfile <- "C:/Users/Bob/Downloads"
Step 3: Download and View File
Next, I’ll use the following code to download the file:
#download file and save in specified destination download.file(url, destfile)
Lastly, I’ll navigate to the Downloads file where I saved the CSV file:

If I double click the file, I can open and view the contents:

Additional Resources
The following tutorials explain how to work with other types of files in R:
Cite this article
stats writer (2024). How can I download files from the internet using R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-download-files-from-the-internet-using-r/
stats writer. "How can I download files from the internet using R?." PSYCHOLOGICAL SCALES, 29 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-download-files-from-the-internet-using-r/.
stats writer. "How can I download files from the internet using R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-download-files-from-the-internet-using-r/.
stats writer (2024) 'How can I download files from the internet using R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-download-files-from-the-internet-using-r/.
[1] stats writer, "How can I download files from the internet using R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I download files from the internet using R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
