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.

1 Like

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.

1 Like

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: )

1 Like

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!

While I get the point about reliance on RStudio, I still do like the getActiveDocumentContext() function. However, I've lately run into the problem that it hangs on me: I have this function in a knitr Rmd file and it used to run fine, but now the function gets called and then RStudio just hangs and can't even be stopped by hitting the stops sign icon. The way to get out is to kill the entire R session.
The switch from working to no longer working happened when I migrated from a RHEL 6 machine with RStudio 1.0.153 to a CentOS 7 machine with RStudio 1.2.1353-1.
Has anyone else experienced this as well - and found a solution?
Btw., when run in the console getActiveDocumentContext() works fine.