Check Whether a File (or Folder) Exists in R
** Download the R syntax and data file used in this post. **
There are certain circumstances when you (as a programmer) may only want to perform an action if a file or folder exists. For instance, you may want to import a data set and use it to perform an analysis, or you need to plot a map but not before downloading a shapefile with the requisite coordinates from your local planning agency. Either way, before proceeding, you need to check if what you need exists.
In R, you can use the file.exists() function to check whether a file or folder exists. The file.exists() function has one main argument:
file: a relative or full path name to a file or folder in a directory.
Download the R syntax and folder for this post. Make sure to close your current R session (if you have one open) and open the R syntax file from the folder to start a new session.)
Now, we are going to check whether a file with the name Teacher Demographics 2016-21.csv exists in our current working directory (which is the Search for a File or Folder folder):
### check if file exists
file.exists(file = "Teacher Demographics 2016-21.csv")
[1] TRUE
TRUE is printed to the console because the file does exist in that folder.
Rather than enter the name of the file in the first argument, you can also create a character vector that contains the name of the file you want to search for and pass it to the file.exists() function like so:
# can also be written as:
file_to_check <- "Teacher Demographics 2016-21.csv"
file.exists(file = file_to_check)
[1] TRUE
The best part? You can incorporate the file.exists() function into an if/else statement that checks if the file exists in a given directory and if it does print a message otherwise, download the file:
# if the file does not exist
if(!file.exists(file_to_check)){
url_download <- "https://cdn.philasd.org/offices/performance/Open_Data/Budget_Staff/Teacher_Demographics/Teacher_Demographics_District_All_Years_20220505.csv"
# download the file to the current directory
destination <- getwd()
download.file(url = url_download, destfile = paste0(destination,"/Teacher Demographics 2016-21.csv"))
# otherwise print a message
}else if(file.exists("Teacher Demographics 2016-21.csv")){
stop("The file already exists in the current directory!")
}
Error: The file already exists in the current directory!
Of course, the error message is printed to the console because the file exists in the current directory.
Do you check whether a file or folder exists before performing a process? Let me know in the comments.
*The Teacher Demographics data set referenced in this post can be downloaded from the School District of Philadelphia's District Performance Office*