[R] Script auto-detecting its own path

Hadley Wickham hadley at rice.edu
Mon Oct 4 21:13:36 CEST 2010


> I'm not sure this will solve the issue because if I move the script, I would
> still have to go into the script and edit the "/path/to/my/script.r", or do
> I misunderstand your workaround?
> I'm looking for something like:
> file.path.is.here("myscript.r")
> and which would return something like:
> [1] "c:/user/Desktop/"
> so that regardless of where the script is, as long as the accompanying
> scripts are in the same directory, they can be easily sourced with something
> like:
> dirX <- file.path.is.here("MasterScript.r")
> source(paste(dirX, "AuxillaryFile.r", sep=""))

If you use relative paths like so:

# master.r
source("AuxillaryFile.r")

Then source("path/to/master.r", chdir = T) will work.  Mastering
working directories is a much better idea than coming up with your own
workarounds.

Hadley

-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/



More information about the R-help mailing list