Get & Set Working Directory in R (3 Examples) | getwd & setwd Functions
This tutorial explains how to identify and change working directories using the getwd and setwd functions in the R programming language.
Table of contents:
So now the part you have been waiting for – the examples.
Definitions & Basic R Syntaxes of getwd and setwd Functions
Definitions: In the following, you can find the definitions of the getwd and setwd functions.
- The getwd R function returns the filepath of the current working directory.
- The setwd R function specifies a new working directory.
Basic R Syntaxes: You can find the basic R programming syntaxes of the getwd and setwd functions below.
getwd() # Basic R syntax of getwd function setwd("Your_Path") # Basic R syntax of setwd function |
getwd() # Basic R syntax of getwd function setwd("Your_Path") # Basic R syntax of setwd function
I’ll explain in the following three examples how to use the getwd and setwd functions in R programming.
Example 1: Get Working Directory Using getwd() Function
In this Example, I’ll illustrate how to return the currently used working directory in R. For this, we can run the getwd command as shown below:
getwd() # Apply getwd function # "C:/Users/Joach/Documents" |
getwd() # Apply getwd function # "C:/Users/Joach/Documents"
As you can see, the getwd function returns a character string containing the filepath of our current working folder to the RStudio console.
Example 2: Set Working Directory Using setwd() Function
The following R syntax explains how to change the working directory using the setwd function in R. Have a look at the following R code:
setwd("C:/Users/Joach/Desktop/my_folder") # Apply setwd function |
setwd("C:/Users/Joach/Desktop/my_folder") # Apply setwd function
We had to specify a character string containing the filepath we want to use within the setwd function. Note that you have to use a slash (not a backslash) to specify your path when you are working on a windows computer.
We can check if the switch to a different working directory worked well by using the getwd function again:
getwd() # Checking new working directory # "C:/Users/Joach/Desktop/my_folder" |
getwd() # Checking new working directory # "C:/Users/Joach/Desktop/my_folder"
Looks good, the working directory was changed.
Example 3: Store Path of Working Directory as Character String in Data Object
In the R programming language it is not necessary to set a working directory explicitly. We can also store the filepath of the working directory we want to use in a data object and use this data object whenever we need it:
my_path <- "C:/Users/Joach/Desktop/my_folder" # Save path |
my_path <- "C:/Users/Joach/Desktop/my_folder" # Save path
The previous R code stored our path in the data object my_path. For instance, we can use this data object to write a csv file to our folder:
write.csv2(1:5, # Write csv file to path paste0(my_path, "/some_data.csv")) |
write.csv2(1:5, # Write csv file to path paste0(my_path, "/some_data.csv"))
Have a look at the folder you have specified. It should contain a new csv file called some_data:
Video, Further Resources & Summary
Do you need more explanations on the R programming codes of this page? Then you may want to watch the following video that I have published on my YouTube channel. In the video, I show the examples of this tutorial.
The YouTube video will be added soon.
In addition, you may want to read some of the other posts of my homepage:
- Set Working Directory to Source File Location Automatically vs. Manually in RStudio
- Check in R if a Directory Exists and Create if It doesn’t
- Determine Path of Current Script in R
- Check Existence of Local File in R
- List All Files with Specific Extension in R
- R Functions List (+ Examples)
- The R Programming Language
In summary: In this R tutorial you learned how to apply the getwd and setwd functions. If you have additional questions, don’t hesitate to let me know in the comments below. Furthermore, don’t forget to subscribe to my email newsletter in order to get updates on the newest articles.