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 https://statisticsglobe.com/.
- Set Working Directory to Source File Location Automatically vs. Manually in RStudio
- Check Existence of Local File in R
- Check in R if a Directory Exists and Create if It doesn’t
- The R Programming Language
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.
Thank you!
Welcome to the Statistics Globe newsletter. From now on, I’ll send you regular emails about statistics, data science, AI, and programming with R and Python.
I’m Joachim Schork. On this website, I provide statistics tutorials as well as code in Python and R programming.
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.
Thank you!
Please check your email inbox and click the confirmation link to complete your subscription. If you don’t see the email within a few minutes, please also check your spam/junk folder.






