[R] Script auto-detecting its own path

Hadley Wickham
Wed, 29 Sep 2010 21:23:24 -0000

>
> Forgive me if this question has been addressed, but I was unable to find
anything in the r-help list or in cyberspace. My question is this: is there a
function, or set of functions, that will enable a script to detect its own path?
I have tried file.path() but that was not what I was looking for. It would be
nice to be able to put all the related scripts I use in the same folder with a
"master" script and then source() them in that "master"
script. Problem is, the "master" script must first know where it is
(without me having to open it and retype the path every time I move it).

Instead of trying to work out where your script is located, when you
source it in, just make sure the working directory is set correctly:

source("/path/to/my/script.r", chdir = T)

chdir is the very useful, but under advertised, argument to source().

Hadley

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University