Get and Set Working Directory in R

In R, the working directory is the folder from which R reads files and to which it writes files when you use a file name without a full path. In this tutorial, we shall learn how to get the current R working directory using getwd() and how to set the R working directory using setwd().

Knowing the working directory is useful when your script reads CSV files, saves plots, writes output files, or loads project resources. Instead of repeatedly typing long file paths, you can set the working directory once and then refer to files by name or by relative path.

What is Working Directory in R?

R Working Directory is the directory of R workspace. Any files in the R workspace could be referenced in R commands without specifying any relative path. While working with external input files or output files, knowing the R workspace helps in easing the efforts.

For example, if the working directory is /home/arjun/workspace/r, then a file named data.csv inside that folder can be read as read.csv("data.csv"). If the file is in another folder, you need to give either a relative path or a complete path.

Syntax of getwd() to Find Current Working Directory in R

The syntax of R function to get working directory is

</>
Copy
getwd()

The getwd() function returns the absolute path of the current working directory as a character string.

Example 1 – Get Working Directory in R using getwd()

In this example, we will use getwd() to get current working directory.

r_wd.R

</>
Copy
# get location of working directory
wd = getwd()
print (wd)

Output

$ Rscript r_wd.R 
[1] "/home/arjun/workspace/r"

The output shows the directory from which the R script is currently running. Your output will be different depending on your operating system, project folder, and how you started R.

Syntax of setwd() to Change Working Directory in R

The syntax of R function to set working directory is

</>
Copy
setwd(<complete_working_directory_path>)

The change to the working directory is only for scope of current running R Script. Once the current R script file execution is completed, the working directory reverts to default workspace.

When you pass a path to setwd(), the folder must already exist. If the folder path is wrong, or if R does not have permission to access it, setwd() returns an error.

Example 2 – Set Working Directory in R using setwd()

In this example, we will use setwd() to set current working directory to a new path.

r_wd.R

</>
Copy
# get location of working directory
wd = getwd()
print (wd)

# set location of working directory
setwd("/home/arjun/")

# verify the working directory
wd = getwd()
cat("\n Working directory changed to : ", wd,"\n")

Output

$ Rscript r_wd.R 
[1] "/home/arjun/workspace/r"

 Working directory changed to :  "/home/arjun"

First, the script prints the initial working directory. Then setwd("/home/arjun/") changes the working directory, and the final getwd() call verifies the new location.

Set R Working Directory Safely After Checking Folder Exists

Before changing the working directory in an R script, it is often safer to check whether the folder exists. This avoids a sudden error when the script is run on another computer or after a folder is renamed.

</>
Copy
new_directory <- "/home/arjun/projects/r-demo"

if (dir.exists(new_directory)) {
  setwd(new_directory)
  print(getwd())
} else {
  stop("Directory does not exist: ", new_directory)
}

Use this pattern when your script depends on a specific folder. It makes the error message clearer and helps you find path problems quickly.

Create a Folder and Set it as R Working Directory

If the folder does not already exist, you can create it with dir.create() and then set it as the working directory.

</>
Copy
project_directory <- "/home/arjun/projects/r-output"

if (!dir.exists(project_directory)) {
  dir.create(project_directory, recursive = TRUE)
}

setwd(project_directory)
print(getwd())

The argument recursive = TRUE allows R to create parent folders too, if they are missing.

Use Relative File Paths from the R Working Directory

After setting the working directory, you can refer to files relative to that folder. This is commonly used for input data, output files, and plots.

</>
Copy
# Current working directory contains a folder named data
customers <- read.csv("data/customers.csv")

# Save output inside a folder named output
write.csv(customers, "output/customers_copy.csv", row.names = FALSE)

Here, data/customers.csv and output/customers_copy.csv are relative paths. R resolves them from the current working directory.

R Working Directory Paths on Windows, macOS, and Linux

Path format can differ by operating system. On Linux and macOS, paths usually look like /home/arjun/project or /Users/arjun/project. On Windows, you may see paths such as C:/Users/Arjun/Documents/project.

In R strings, forward slashes are usually easier to use on Windows too.

</>
Copy
setwd("C:/Users/Arjun/Documents/r-project")

If you use backslashes in a Windows path, escape each backslash because \ has a special meaning inside R strings.

</>
Copy
setwd("C:\\Users\\Arjun\\Documents\\r-project")

Build Working Directory File Paths with file.path()

Instead of joining folder names manually, use file.path(). It builds paths using the correct file separator for the operating system.

</>
Copy
base_directory <- getwd()
input_file <- file.path(base_directory, "data", "sales.csv")

print(input_file)

This is more reliable than pasting path parts with paste(), especially when the same script is used on different operating systems.

Common setwd() Error in R: Cannot Change Working Directory

A common error while using setwd() is shown below.

Error in setwd("path/to/folder") : cannot change working directory

This usually means one of the following:

  • The folder path does not exist.
  • The path contains a typing mistake.
  • The folder name has spaces or special characters and the path was not written correctly.
  • R does not have permission to access that folder.
  • A Windows path was written with unescaped backslashes.

Check the folder path with dir.exists() before calling setwd().

</>
Copy
dir.exists("/home/arjun/projects/r-demo")

RStudio Projects and Working Directory

If you work in RStudio, an RStudio Project is often a better long-term approach than manually calling setwd() in every script. When you open an RStudio Project, the project folder becomes the natural starting point for your files.

This keeps scripts more portable because the project can be opened from different computers without hardcoding a user-specific path such as /home/arjun/ or C:/Users/Arjun/.

Practical Tips for R Working Directory in Scripts

  • Use getwd() first when you are unsure where R is reading or writing files.
  • Use setwd() only when the script really needs a fixed working directory.
  • Prefer project-relative paths for scripts that will be shared with others.
  • Use file.path() instead of manually combining folder names.
  • Check folders with dir.exists() before changing the working directory.

Frequently Asked Questions on R Working Directory

How do I find the working directory in R?

Use getwd() to find the current working directory in R. It returns the folder path from which R reads and writes files when relative paths are used.

How do I set the working directory in R?

Use setwd("path/to/folder") to set the working directory in R. The folder must already exist, unless you create it first with dir.create().

What is the working directory in R used for?

The working directory is used as the base folder for relative file paths. It helps R locate input files and decide where to save output files when a complete path is not provided.

Why does setwd() show cannot change working directory?

This error usually occurs when the folder path is incorrect, the folder does not exist, the path is not written correctly, or R does not have permission to access the folder.

Can I create a working directory in R?

Yes. Use dir.create() to create a folder, and then use setwd() to make that folder the current working directory.

QA Checklist for R Working Directory Tutorial

  • Confirmed that getwd() is explained as the function used to get the current R working directory.
  • Confirmed that setwd() examples use quoted folder paths.
  • Included a warning that the folder must exist before calling setwd().
  • Added Windows path guidance for forward slashes and escaped backslashes.
  • Included practical examples for dir.exists(), dir.create(), relative paths, and file.path().

Summary of getwd() and setwd() in R

In this R Tutorial, we have learnt how to set working directory and get the value of working directory using example R scripts. Use getwd() to check where R is currently working, and use setwd() to change the working directory when your script needs to read or write files from a specific folder.