Set Working Directory to Source File Location Automatically vs. Manually in RStudio (2 Examples)
In this R post you’ll learn how to set the R working directory to the source file location (i.e. the folder where your currently used R file is stored). I’ll show you how to do that manually by clicking in the RStudio interface or automatically with some R code.
Table of contents:
- Setting Up the Examples
- Example 1: MANUALLY Set Working Directory to Source File Location
- Example 2: AUTOMATICALLY Set Working Directory to Source File Location
- Video, Further Resources & Summary
Let’s jump right to the examples…
Setting Up the Examples
For comparison, we should check the current working directory before changing the working directory to the source file location. We can do that with the getwd function as shown below:
getwd() # Check current working directory # "C:/Users/Joach/Desktop"
If you run the previous code, the RStudio console is returning the currently used working directory, i.e. “C:/Users/Joach/Desktop”.
In the following two examples, you’ll learn how to change the working directory to the source file location manually and automatically. Let’s start with the manual approach…
Example 1: MANUALLY Set Working Directory to Source File Location
If you want to set the working directory of your RStudio session to the source file location manually (i.e. by clicking), you can do the following:
Step 1) Click on the Session tab.
Step 2) Click on Set Working Directory > To Source File Location.
Afterwards, you working directory will be changed to the location of your source file.
Example 2: AUTOMATICALLY Set Working Directory to Source File Location
In case you want to automatize the definition of your working directory, it makes a lot of sense to set the working directory to source file location with some R programming code. For this task, we first need to load the rstudioapi package:
library("rstudioapi") # Load rstudioapi package
Now, we can combine the setwd, dirname, and getActiveDocumentContext functions to change our working directory to the source file location:
setwd(dirname(getActiveDocumentContext()$path)) # Set working directory to source file location
Let’s check if it worked:
getwd() # Check updated working directory # "C:/Users/Joach/Desktop/example_dir"
Video, Further Resources & Summary
Do you need further info on the R programming codes of this tutorial? Then you might want to have a look at the following video of my YouTube channel. I’m explaining the R code of this post in the video.
Furthermore, I can recommend to have a look at some of the other tutorials of my website:
- Check in R if a Directory Exists and Create if It doesn’t
- List All Files with Specific Extension in R
- Getting & Setting Working Directories
- R Functions List (+ Examples)
- The R Programming Language
To summarize: At this point you should have learned how to use the location of your current R script as working directory by clicking or by an automatized command line in the R programming language. Tell me about it in the comments section, if you have any further questions.