[R] Path to R script
d.scott at auckland.ac.nz
Thu May 13 01:18:59 CEST 2010
David Scott wrote:
> Sharpie wrote:
>> Johannes W. Dietrich wrote:
>>> There is certainly a trivial solution for my question, but I can't
>>> find the answer in the documentation.
>>> I need a platform independent method to obtain the file path of the
>>> current R script.
>>> My working group uses R on several machines with different operating
>>> systems including Mac OS X, Windows and Linux, and for obvious
>>> reasons the file hierarchies are very different. Therefore, it would
>>> be useful to be able to address data files and external procedures
>>> relative to the path of the script.
>>> Thank you for any suggestion,
>>> J. W. D.
>> The best recipie I have for dealing with this situation is:
>> 1. Place R scripts in a package (a very good idea anyway if you are
>> distributing code to others).
>> 2. Place a configuration file in the package that contains info such as
>> paths to data directories.
>> 3. Customize the configuration file after installing the package (if
>> Then your script accesses the configuration file, which is in a known fixed
>> location, and uses the information inside to find other files that may be in
>> unknown and changing locations.
>> You could also place the configuration file in a location such as:
>> path.expand( '~/' )
>> Has a pretty predictable behavior across platforms. This would save it from
>> being overwritten every time you updated the package.
>> Hope this helps!
> I also was about to suggest putting the code in a package because then
> you have system.file available, which I presume works across platforms:
> > system.file("RunTests.R",package="DistributionUtils")
>  "/usr/local/david/R/DistributionUtils"
> > system.file("unitTests/runTests.R",package="DistributionUtils")
>  "/usr/local/david/R/DistributionUtils/unitTests/runTests.R"
Sorry, I copied that incorrectly. Should be:
David Scott Department of Statistics
The University of Auckland, PB 92019
Auckland 1142, NEW ZEALAND
Phone: +64 9 923 5055, or +64 9 373 7599 ext 85055
Email: d.scott at auckland.ac.nz, Fax: +64 9 373 7018
Director of Consulting, Department of Statistics
More information about the R-help