How can a source
d or Sweave
d file find out its own path?
Background:
I work a lot with .R scripts or .Rnw files. My projects are organized in a directory structure, but the path of the project's base directory frequently varies between different computers (e.g. because I just do parts of data analysis for someone else, and their directory structure is different from mine: I have projects base directories ~/Projects/StudentName/ or ~/Projects/Studentname/Projectname and most students who have just their one Project usually have it under ~/Measurements/ or ~/DataAnalysis/ or something the like - which wouldn't work for me).
So a line like
setwd (my.own.path ())
would be incredibly useful as it would allow to ensure the working directory is the base path of the project regardless of where that project actually is. Without the need that the user must think of setting the working directory.
Let me clarify: I look for a solution that works with pressing the editor's/IDE's source
or Sweave
Keyboard shortcut of the unthinking user.
I have no direct solution how to obtain the directory of the file itself but if you have a limited range of directories and directory structures you can probably use
You could check out the pattern of the directory in question and then set the dir. Does this help you?
This answer works for
source
and also insidenvim-R
- I have no idea if it works withknitr
and similar things. Any feedback appreciated.If you have multiple scripts
source
-ing each other, it is important to get the correct one. That is, the largesti
for whichsys.frame(i)$ofile
exists.Starting from gsk3's Seb's suggestions, here's an idea:
That leads to something like:
So there is an automatic solution, that
source
Sweave
even works for interactive sessions where the commands are sent line by line
the combination of
user
andnodename
of course needs to be specificImprovements welcome!
Update:
Gabor Grothendieck answered the following to a related question on r-help today:
which will work for
source
.Another update: I now do most of the data analysis work in RStudio. RStudio's projects basically solve the problem: RStudio changes the working directory to the project root directory every time I switch between projects.
I can therefore put the project directory as far down my directory tree as I want (and the students can also put their copy wherever they want) and sync the data files and scripts/
.Rnw
s via version control (We use a private git server). The RStudio project files are kept out of the version control, i.e..gitignore
contains.Rproj.user
.Obviously, within the project, the directory structure needs to be synchronized.
Just FYI,
knitr
willsetwd()
to the dir of the input file when (and only when) evaluating the code chunks, i.e. if you callknit('path/to/input.Rnw')
, the working dir will be temporarily switched topath/to/
. If you want to know the input dir in code chunks, currently you can call an unexported functionknitr:::input_dir()
(I may export it in the future).You can use
sys.calls()
to get the command used to source the file. Then you need a bit of trickery using regular expressions to get the pathname, bearing in mind thatsource("something/filename")
could have used either the absolute or relative path. Here's a first attempt at putting all the pieces together: try inserting the following lines at the top of a source file.It's not very robust—for instance, it will fail on windows with
source("pathname\\filename")
, and I haven't tested what happens if you have one file sourcing another file—but you might be able to build a solution on top of this.An additional problem is that the working directory is a global variable, which can be changed by any script, so if your script calls another script, it will have to set the wd back. In RStudio I use Session -> Set Working Directory -> To Source File Location (I know, it's not ideal), and then my script does
In this way one can maintain a stack of working directories. I would love to see this implemented in the language itself.