How to Use setwd() Function in R

The working directory can be your best friend or ruthless adversary. Confusion about the working directory is a great time waster. Whenever you want to perform some advanced action, you need to ensure that you are in the correct directory. Otherwise, it can create an issue for you.

getwd in R

The getwd in r is a built-in function that returns the absolute filepath representing the current working directory of the R process. Let’s talk about the setwd() function in R Language.

setwd in R

The setwd in r is a built-in function that changes the current directory as a working directory. The setwd() method takes a new working directory as an argument. To change the current working directory in R, use the setwd() function.

The setwd() function is generally used to navigate the data directory.




The function takes dir as an argument, a character string: tilde expansion will be done.

Return Value

The setwd() method returns the current directory before the change, invisibly and with the same conventions as the getwd() function. It will give an error if it does not succeed.


To change the current working directory, use the setwd() function. The setwd() function requires the new working directory as an argument to the function.


The setwd() function is used to point at a particular directory.

In this example, we had to specify a character string containing the filepath we want to use within the setwd function. This is because you have to use a slash (not a backslash) to specify your path when working on a windows computer.

Set working directory in RStudio

For the first time you use R, the suggested procedure, under Windows and MAC OSX, is as follow:

  1. Create a sub-directory, say R, in your “Documents” folder. This sub-folder, also known asworking directory, will be used by R to read and save files.
  2. Launch R by double-clicking on the icon.
  3. Specify your working directory to R:
    • On Windows: File –> Change directory
    • On MAC OSX: Tools –> Change the working directory

Advance example of R setwd() Function

The setwd() function has uses beyond just set the current working directory. However, the following points allow you to iterate and execute a set of scripts.

  1. The setwd() function is used to point at a particular directory.
  2. The list.files() function is used to grab everything in the directory and subfolders.
  3. Then we use the pattern match to get the scripts we need.
  4. Use the source function to execute the R code.

Let’s write the code based on these scenarios.

# setwd() Function - To set the working directory your scripts folder
setwd("/your scripts directory")

# keep only the files that END with ".R" or ".r" using list.files() method and 
# pass the match parameter
script_list <- list.files(pattern = ".*\\.[rR]$", recursive = TRUE)

# loop through and trigger scripts using source ()
for (i in script_list)

Here, we are getting a list of files whose extension is .R.

First, we set a current working directory using the setwd() function and pass the path you want to set.

Then we are using the list.files() function, which accepts the pattern argument, which will return all the files recursively and make a list of them.

In the final step, we loop through that list one by one and trigger it using the source() function.

Also, each script is executing within its subfolder. This allows us to load/use different versions of any supporting scripts and data sets.

If you want to run an R script from a batch file to automate the same process and set a working directory in the batch file using the setwd() method.


R provides a function called setwd() which can help you to set the current directory.

R provides a function called getwd() which returns the working directory.

That is it for the setwd() function.

More posts

Check if a folder exists in R

Move files in R

Rename a file in R

unlink in R

Leave a Comment