Check in R if a Directory Exists and Create if It doesn’t (Programming Example)

 

This R tutorial explains how to check for the existence of a directory and create a new directory if it doesn’t exist.

The article contains the following sections:

Let’s dive right into the examples…

 

Store Working Directory

Before we can start with the examples, it is helpful to store the working directory you want to use in a new data object. Let’s do this:

main_dir <- "C:/Users/ ... Your Path ... /main_dir"   # Set your working directory

The previous R code stored our main working directory in the data object main_dir. Note that you have to replace the … Your Path … with your own directory path.

Let’s have a look on our example directory:

 

main directory

Figure 1: Example Directory with One Already Existing Sub-directory.

 

As you can see based on Figure 1, our example directory contains one sub-directory with the name subdir_example.

 

Check for Existence of Sub-directory

In this section, we will try to create a new directory, but only in case the directory name does not exist yet. First, we need to specify the name of the sub-directory that we want to create. Let’s assume that we want to create the folder subdir_example (i.e. a folder with the same name as the folder that already exists).

sub_dir_exists <- "subdir_example"                    # Name of already existing folder

With the following R code, we can check for the existence of this directory and create it in case it doesn’t exist:

dir.create(file.path(main_dir, sub_dir_exists))       # Try to create already existing folder
# Warning message:
#   In dir.create(file.path(main_dir, sub_dir_exists)) :
#   'C:\Users\ ... Your Path ... \main_dir\subdir_example' already exists

The RStudio console returns a warning message:

In dir.create(file.path(main_dir, sub_dir_exists)) :
‘C:\Users\ … Your Path … \main_dir\subdir_example’ already exists

If you see this warning message, you know that a file with your specified name already exists and that no new folder was created.

 

Create New Sub-directory

If we want to create a new sub-directory, we need to specify a new name that does not exist yet:

sub_dir_new <- "subdir_new"                           # Name of new folder

Now, we can apply exactly the same R syntax as in the previous section…

dir.create(file.path(main_dir, sub_dir_new))          # Create new folder

…but this time we do not get a warning – The new folder was successfully created:

 

new sub-directory created by r programming language

Figure 2: Example Directory with New Sub-directory.

 

Video Tutorial & Further Resources

On my YouTube channel, I have published a live programming tutorial, which contains the examples of this article. In case you are interested, you can find the video tutorial below:

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

Also, you might want to have a look at the other R tutorials of this homepage. You can find some recommended tutorial below:

This page explained how to create a directory in RStudio in case it does not exist yet. If you have any further questions, please let me know in the comments below.

 

Subscribe to my free statistics newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top