[R] Genuine relative paths with R

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Sat Oct 6 23:19:32 CEST 2018


Please use reply-all to keep the conversation on the mailing list.

I found no enlightenment in your response to Ista. 

a) I have many years of experience in a variety of operating systems and languages, and there are a variety of approaches taken to handle self-configuration. R is not special in having to cede some responsibility to the OS if you want portability. I have no problems making my R code work unchanged on various platforms (and in multiple directories on the same machine of needed) simply by making all my path references local.

b) If you want to communicate to us about what mechanisms you cannot find in R, you are going to have to use more specific words than "this function". Ista already mentioned setwd, which works perfectly fine with relative paths, and the (optional) file.path function can construct paths without even assuming that "/" is the separator. Regardless of language I find that changing the working directory as the script runs makes for brittle code... simply using paths relative to the working directory avoids the risk of leaving the current directory changed if the processing fails.

c) Note that R does not provide elaborate support for sharing directories full of scripts containing functions... if you want to build complex code you should be building packages and using the R CMD check facility to prepare your code for use in other environments.

d) Some wild guesses at what you might find useful: ?Startup, littler package, ?commandArgs.

On October 6, 2018 1:43:34 PM PDT, Olivier GIVAUDAN <olivier_givaudan using hotmail.com> wrote:
>Hi Jeff,
>
>Thanks for sharing your workaround.
>
>I guess my last answer to Ista answers your question as well.
>
>To me this function (an equivalent of 'cd', say) should be
>platform-independent.
>
>Best regards,
>
>Olivier
>________________________________
>De : Jeff Newmiller <jdnewmil using dcn.davis.ca.us>
>Envoyé : samedi 6 octobre 2018 19:31
>À : r-help using r-project.org; Olivier GIVAUDAN; r-help using r-project.org
>Objet : Re: [R] Genuine relative paths with R
>
>I stopped using hardcoded absolute paths inside R scripts years ago,
>and I suspect that is fairly common practice. That is, I almost never
>enter a path starting with "/" or "c:/" in an R script.
>
>The key concession you have to make is to start your R session in your
>working directory using OS-specific mechanisms, and then reference your
>code and data relative to that directory. RStudio project files offer
>one mechanism for doing this; using CD from the OS command line is
>another, and using the file-browser double-click mechanism on .RData
>files is another (though I prefer to avoid that these days due to
>potential global environment contamination).
>
>Perhaps you can be more specific about what facilities you are
>expecting to find. You should also mention what OS you typically use
>and how you normally start R.
>
>On October 6, 2018 4:48:44 AM PDT, 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:
>>https://stackoverflow.com/questions/1815606/rscript-determine-path-of-the-executing-script
>>https://stackoverflow.com/questions/47044068/get-the-path-of-current-script/47045368
>>http://r.789695.n4.nabble.com/Script-auto-detecting-its-own-path-td2719676.html
>>
>>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,
>>
>>Olivier
>>
>>       [[alternative HTML version deleted]]
>>
>>______________________________________________
>>R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>https://stat.ethz.ch/mailman/listinfo/r-help
>>PLEASE do read the posting guide
>>http://www.R-project.org/posting-guide.html
>>and provide commented, minimal, self-contained, reproducible code.
>
>--
>Sent from my phone. Please excuse my brevity.

-- 
Sent from my phone. Please excuse my brevity.




More information about the R-help mailing list