Determine Path of Current Script in R (Example)


In this tutorial, I’ll explain how to find the file location of the currently executing R script in R programming.

Table of contents:

Let’s dig in…


Example: Get File Location of Currently Opened R Script

In this Example, I’ll illustrate how to return the working directory of the currently used R script.

We first have to install and load the rstudioapi package:

install.packages("rstudioapi")   # Install rstudioapi package
library("rstudioapi")            # Load rstudioapi package

Now, we can use the getSourceEditorContext function to identify the file path of the executing R script:

getSourceEditorContext()$path    # Applying getSourceEditorContext
# "C:/Users/Joach/Desktop/Example_File.R"

As you can see based on the output of the RStudio console, the script is currently stored on the desktop of my computer: “C:/Users/Joach/Desktop/Example_File.R”


Video, Further Resources & Summary

Do you want to learn more about file locations? Then I can recommend to have a look at the following video of my YouTube channel. I’m explaining the R programming syntax of this article in the video.


The YouTube video will be added soon.


In addition, I can recommend to have a look at the related tutorials on


Summary: At this point you should have learned how to identify the currently used file path in the R programming language. Don’t hesitate to let me know in the comments section, in case you have additional comments and/or 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.

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.