[R] Genuine relative paths with R
o||v|er_g|v@ud@n @end|ng |rom hotm@||@com
Wed Oct 10 15:53:00 CEST 2018
1. By definition the relative path (I'm excluding the absolute path solution for obvious reasons) depends on the current working directory: What if my R script is not located along this current working directory? It won't work.
2. What should I write as an option in this .Rprofile file? An absolute path to the project's root? Plus I don't want the users to choose their working directory: this technicality should be kept hidden from them and be automatic.
De : Gabor Grothendieck <ggrothendieck using gmail.com>
Envoyé : samedi 6 octobre 2018 23:33
À : olivier_givaudan using hotmail.com; r-help using r-project.org
Objet : Re: [R] Genuine relative paths with R
1. Assuming you are starting the script from within R, if you want to
keep all the files used by the script together with the script itself
then just source the script using the absolute or relative path to the
script using source(..., chdir = TRUE) as shown below and the script
will run in the directory containing the script. We used an absoluate
path below but a relative path will work too. In either case the
script itself will not need to use absolute paths and it is portable
to other machines.
source("/path/to/script.R", chdir = TRUE)
If your script is on your PATH then this would work:
source(Sys.which("script.R"), chdir = TRUE)
2. Another approach is to define an R option, say root, using the R
options() function to define the root directory of your project. You
can have a different R option for each project. Place the options()
statements to set these R options for your various projects in your
.Rprofile, say, and in the script use:
root <- getOption("root", ".")
to cause it to retrieve the value of the R option root if it is
defined and use the current directory otherwise. Use a different name
for each project. If the user does not define the R option root it
will be up to them to change directory first. Again there will be no
use of absolute paths in the script itself and it is portable to other
What is particularly convenient about this is that it documents where
all the projects are on the machine right in the .Rproject file so
one always knows where they are.
On Sat, Oct 6, 2018 at 8:25 AM Olivier GIVAUDAN
<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?
> Best regards,
> [[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.
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
email: ggrothendieck at gmail.com
[[alternative HTML version deleted]]
More information about the R-help