[R] Genuine relative paths with R
m@cqueen1 @end|ng |rom ||n|@gov
Mon Oct 8 18:04:47 CEST 2018
A few facts (and some opinions):
R understands relative paths (as do the other languages you mentioned) (you have misunderstood R if you think it doesn't)
R interprets relative paths as being relative to its current working directory
To find out R's current working directory type: getwd() at the R prompt
There are various ways to set R's working directory, depending on how you are running R. Once R is running, you can if necessary use setwd() at the R prompt, and this is platform-independent (but it should rarely be necessary).
The easiest way to set the working directory, in my opinion, is by starting R from a linux or Mac command shell (bash, tcsh, etc) environment. You start R by typing "R" at the shell prompt and R's working directory is inherited from the current working directory of the shell.
If you are using a GUI (i.e., you started R by double-clicking on some icon), look for menu commands to set the working directory. Check the documentation for the GUI to find out other ways to set the working directory for that GUI.
No additional packages are necessary in order to manage R's working directory simply and effectively.
Yes, you can move all the scripts related to some project as a whole to some other location and have them run seamlessly using relative paths. Hopefully it is clear by now that all that is needed is to set R's working directory to the project's main directory.
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
Lab cell 925-724-7509
On 10/6/18, 4:48 AM, "R-help on behalf of Olivier GIVAUDAN" <r-help-bounces using r-project.org on behalf of olivier_givaudan using hotmail.com> wrote:
Dear R users,
I would like to work with genuine relative paths in R for obvious reasons: if I move all my scripts related to some project as a whole to another location of my computer or someone else's computer, if want my scripts to continue to run seamlessly.
What I mean by "genuine" is that it should not be necessary to hardcode one single absolute path (making the code obviously not "portable" - to another place - anymore).
For the time being, I found the following related posts, unfortunately never conclusive or even somewhat off-topic:
So I found 2 workarounds, more or less satisfactory:
1. Either create a variable "ScriptPath" in the first lines of each of my R scripts and run a batch (or shell, etc.) to replace every single occurrence of "ScriptPath <-" by "ScriptPath <- [Absolute path of the R script]" in all the R scripts located in the folder (and possibly subfolders) of the batch file.
2. Or create an R project file with RStudio and use the package "here" to get the absolute path of the R project file and put all the R scripts related to this project in the R project directory, as often recommended.
But I am really wondering why R doesn't have (please tell me if I'm wrong) this basic feature as many other languages have it (batch, shell, C, LaTeX, SAS with macro-variables, etc.)?
Do you know whether the language will have this kind of function in a near future? What are the obstacles / what is the reasoning for not having it already?
Do you know other workarounds?
[[alternative HTML version deleted]]
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
More information about the R-help