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.
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
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 @jennybryanhas 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
(The only downside of it is that the word 'here' starts to lose all meaning really quickly )
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.
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.