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):
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:
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:
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*