[R] Script auto-detecting its own path

Hadley Wickham hadley at rice.edu
Wed Sep 29 23:23:24 CEST 2010


>
> 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
http://had.co.nz/



More information about the R-help mailing list