Get full file path of current script


I’m looking for a clean way to access the full file path of the script currently being run. This should be independent of the working directory.

Some other conversations on this are:

Tagging @jennybryan as we started a conversation about this earlier.


My interest in this topic includes whether or when one should need such a function. Workflow-wise, it sets off some red flags re: practices around managing working directory and putting reusable logic in a package, parametrized .Rmd, or similar. I hope the discussion helps to identify reasons (good, bad, or neutral) why a script might need to access its own location in the filesystem.


whether or when one should need such a function

Any other programming language provides this. So from a UX perspective, as in "what does the user expect to be able to do" it should be there.

Then, a regular use-case would be that persons A and B work on the same script using git and git lfs. "git clone" sets up the workspace. Imagine a file structure

           | --- script.R
           | --- data/
                     | --- data.csv

The script should be able to do something like

setwd( system.getCurrentDirectory() )  
data <- read.csv(file="data/data.csv")

AFAIK right now A and B will have to maintain a separate launcher script that sets the correct working directory, which is bad, or find-and-replace all absolute paths, which is worse.


One method many people use is to make the root folder (one that would be shared between collaborators) an RStudio project. This will always set the working directory to the root folder, regardless of where the folder lives on your (or anybody else's) system. This strategy in conjunction with the here package might be a worthwhile workaround for what you want.


The here package, which @jennybryan has championed before, covers this use case really, really well! It's very close to the top of my 'things to do better on my next analysis' list :stuck_out_tongue:

(The only downside of it is that the word 'here' starts to lose all meaning really quickly :wink: )


I wrote down reasons why explicit setwd() is generally not a good idea in the long-run and how it can be avoided:


rstudioapi package has getActiveDocumentContext function

current_working_dir <- dirname(rstudioapi::getActiveDocumentContext()$path)


Relying on the rstudioapi package will lead to a script that can only be run inside RStudio, though, which is problematic. The various ways the here package recognizes the top-level of a project are file-based, which makes the project more portable.


That's a great point! Thank you very much for the tip!