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:

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.

session tab rstudio

 

Step 2) Click on Set Working Directory > To Source File Location.

set working directory to source file rstudio

 

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"

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.

 

The YouTube video will be added soon.

 

Furthermore, I can recommend to have a look at some of the other tutorials of my website:

 

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.

 



Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top