Set Working Directory To Source File Location In RStudio ...
Maybe your like
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 packageNow, 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 locationLet’s check if it worked:
getwd() # Check updated working directory # "C:/Users/Joach/Desktop/example_dir"Looks good!
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.
Subscribe to the Statistics Globe Newsletter
Get regular updates on the latest tutorials, offers & news at Statistics Globe. I hate spam & you may opt out anytime: Privacy Policy.
Tag » How To Set Working Directory In R
-
Running RStudio And Setting Up Your Working Directory - STHDA
-
Getwd, Setwd | R Function Of The Day
-
How To Change Working Directory In R - Universe Of Data Science
-
Working Directories And Workspaces In The RStudio IDE
-
SET And GET Working Directory In R [setwd() And Getwd() Functions]
-
Get Or Set Working Directory - R
-
1.7 Working Directories | An Introduction To R
-
Setting Up Working Directory In R | R Tutorial 1.11 - YouTube
-
9.2 The Working Directory - YaRrr! The Pirate's Guide To R
-
Changing Working Directories In R | R - DataCamp
-
Chapter 12 Setting A Working Directory | R For HR: An Introduction To ...
-
Get And Set Working Directory In R - GeeksforGeeks
-
How To Set Working Directory | Introduction To Quantitative Methods
-
7. Import And Save Data - R Tutorial