R: How to Create Directory if It Doesn’t Exist


Often you may want to create a directory in R only if it does not already exist.

You can use the following basic syntax to do so:

ifelse(!dir.exists(file.path(new_dir)),
        dir.create(file.path(new_dir)),
        "Directory Exists")

This particular example uses an ifelse() statement to perform the following actions:

  • First, check if the directory specified by the new_dir variable exists.
  • If the directory does not exist, then create it.
  • If the directory does exist, then output “Directory Exists” to the user.

Note that the new_dir variable is assumed to be a string that contains the full path to the new directory that you wish to create.

Also note that the ifelse() function comes built-in with base R so you don’t need to install or load any external packages to run this code.

The following example shows how to use this syntax in practice.

Example: How to Create Directory if it Does Not Already Exist in R

Suppose that we would like to create a directory with the following path:

  • c:/users/bobbi/dat/central-data

Before we create this directory, we can use the getwd() function to first get the path of the current working directory:

#get path of current working directory
getwd()

[1] "c:/users/bobbi/dat"

The getwd() function returns the following path to the current working directory:

  • c:/users/bobbi/dat

We can then use the list.files() function to view all folders and files in this current working directory:

#list all folders and files in current working directory
list.files()

[1] "east-data"  "north-data" "south-data" "west-data" 

We can see that there are four folders in this current directory with the following names:

  • east-data
  • north-data
  • south-data
  • west-data

Suppose that we would like to create a new directory in this current working directory with the name central-data.

We can use the following syntax to do so:

#specify path to new directory
new_dir <- "c:/users/bobbi/dat/central-data"

#create directory if it does not already exist
ifelse(!dir.exists(file.path(new_dir)),
        dir.create(file.path(new_dir)),
        "Directory Exists")

[1] TRUE

The code returns TRUE, which indicates that the new directory was successfully created.

We can then use the list.files() function again to view all folders and files in the current directory:

#list all folders and files in current working directory
list.files()

[1] "central-data" "east-data"    "north-data"   "south-data"   "west-data"    

We can see that a new directory has been created named central-data.

Suppose instead that we attempt to create a directory that already exists. For example, suppose we attempt to create a new directory named south-data, which already exists in the current working directory:

#specify path to new directory
new_dir <- "c:/users/bobbi/dat/south-data"

#create directory if it does not already exist
ifelse(!dir.exists(file.path(new_dir)),
        dir.create(file.path(new_dir)),
        "Directory Exists")

[1] "Directory Exists"

The code returns “Directory Exists” since  a directory named south-data already exists in this current working directory.

Note that you can choose to return any string that you would like in the ifelse() function to alert the user that a particular directory already exists.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Load Multiple Packages in R
How to Check if a Package is Installed in R
How to Clear the Environment in R
How to Clear All Plots in RStudio

Featured Posts

Leave a Reply

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