How to Set Location of Current File as Working Directory in R Studio

Jesse John Feb 02, 2024
  1. Use R Studio’s File Association
  2. Use an R Project File
  3. Use the R Studio API Package to Get the Current Saved File Path in R Studio
  4. Use the this.path Package to Get the Path of the Current R File
  5. Conclusion
How to Set Location of Current File as Working Directory in R Studio

This article explores four different ways to set R’s working directory to the current .R script or .Rmd file that is open in the source pane of R Studio. No user intervention or interaction is required in any of these methods.

This allows us to access other script files or data that we may have stored in the same directory using just their file name, without a path. In this article, the words folder and directory are used interchangeably.

Use R Studio’s File Association

When R Studio is launched by right-clicking or double-clicking a .R or .Rmd file, it sets the working directory to the folder containing the opened file.

This only works when R Studio is launched that way. If R Studio is already running, and only the file is opened by right or double-clicking, the working directory is not changed.

Use an R Project File

We can create a new R Project and associate it with the folder containing our .R script or .Rmd file. R Studio will create a .Rproj file in that folder.

When we launch the .Rproj project file, R Studio will change the working directory to that folder.

Use the R Studio API Package to Get the Current Saved File Path in R Studio

We can use the getSourceEditorContext() function from the rstudioapi package to get the current saved file path. We can then set the path to its directory as the working directory.

To use this function, we have to first install the package.

Example Code:

# Check the current working directory.
getwd()

# Install the rstudioapi package if it is not already installed.
install.packages("rstudioapi")

# Load the rstudioapi package.
library(rstudioapi)

# Get the name of the directory in which the current file is located.
cur_dir = dirname(getSourceEditorContext()$path)

# Change the working directory to the directory with the current file.
setwd(cur_dir)

# Check that the working directory has changed to the directory with the current file.
getwd()

Use the this.path Package to Get the Path of the Current R File

Finally, we will use the this.path package to get the path to the current, saved file. This method is more general than the first three; it works even when not using R Studio.

The approach is the same; we will get the path to the current file using the this.path() function. We then set the path of its directory as the working directory.

Example Code:

# Check the working directory.
getwd()

# Install the this.path package.
install.packages("this.path")

# Load the this.path package.
library(this.path)

# Get the directory from the path of the current file.
cur_dir2 = dirname(this.path())

# Set the working directory.
setwd(cur_dir2)

# Check that the working directory has been set as desired.
getwd()

References

Learn about R Studio Working Directories and Workspaces here. To learn more about R Projects, look at R for Data Science.

Documentation for the rstudioapi and this.path packages is available on CRAN.

Conclusion

Using different techniques, we can set the working directory to the folder containing the current R file. The file association method and the R Project method are readily available in R Studio.

The rstudioapi method requires that the package be installed. The this.path package is more widely applicable but requires the installation of that package.

Author: Jesse John
Jesse John avatar Jesse John avatar

Jesse is passionate about data analysis and visualization. He uses the R statistical programming language for all aspects of his work.

Related Article - R File