Dear Eik, Thank you for your message. Yes, I already got to know this package and this discussion (while I found the 'here' package). But thanks. Best regards, Olivier ________________________________ De : Eik Vettorazzi <E.Vettorazzi at uke.de> Envoy? : lundi 8 octobre 2018 11:30 ? : Olivier GIVAUDAN; r-help at r-project.org Objet : Re: [R] Genuine relative paths with R Dear Olivier, you may find the rprojroot package useful, see https://github.com/r-lib/rprojroot and a discussion https://gist.github.com/jennybc/362f52446fe1ebc4c49f#file-2014-10-12_stop-working-directory-insanity-md Cheers Am 06.10.2018 um 13:48 schrieb Olivier GIVAUDAN:> 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 at 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. >-- Eik Vettorazzi Department of Medical Biometry and Epidemiology University Medical Center Hamburg-Eppendorf Martinistrasse 52 building W 34 20246 Hamburg Phone: +49 (0) 40 7410 - 58243 Fax: +49 (0) 40 7410 - 57790 Web: www.uke.de/imbe<http://www.uke.de/imbe> -- _____________________________________________________________________ Universit?tsklinikum Hamburg-Eppendorf; K?rperschaft des ?ffentlichen Rechts; Gerichtsstand: Hamburg | www.uke.de Vorstandsmitglieder: Prof. Dr. Burkhard G?ke (Vorsitzender), Prof. Dr. Dr. Uwe Koch-Gromus, Joachim Pr?l?, Martina Saurin (komm.) _____________________________________________________________________ SAVE PAPER - THINK BEFORE PRINTING [[alternative HTML version deleted]]
If you are always running your main script as 'Rscript myscript.R' then your file argument will appear as '--file=myscript.R' in commandArgs(). So you can use something like scriptFile <- function() { pat <- "^--file=" args <- commandArgs() file <- args[grepl(pat, args, args)] normalizePath(sub(pat, "", file)) } Then scriptFile() will give you something to work with. Best, luke On Wed, 10 Oct 2018, Olivier GIVAUDAN wrote:> Dear Eik, > > Thank you for your message. > > Yes, I already got to know this package and this discussion (while I found the 'here' package). But thanks. > > Best regards, > > Olivier > ________________________________ > De : Eik Vettorazzi <E.Vettorazzi at uke.de> > Envoy??? : lundi 8 octobre 2018 11:30 > ??? : Olivier GIVAUDAN; r-help at r-project.org > Objet : Re: [R] Genuine relative paths with R > > Dear Olivier, > you may find the rprojroot package useful, see > https://github.com/r-lib/rprojroot > > and a discussion > > https://gist.github.com/jennybc/362f52446fe1ebc4c49f#file-2014-10-12_stop-working-directory-insanity-md > > Cheers > > Am 06.10.2018 um 13:48 schrieb Olivier GIVAUDAN: >> 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 at 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. >> > > -- > Eik Vettorazzi > > Department of Medical Biometry and Epidemiology > University Medical Center Hamburg-Eppendorf > > Martinistrasse 52 > building W 34 > 20246 Hamburg > > Phone: +49 (0) 40 7410 - 58243 > Fax: +49 (0) 40 7410 - 57790 > Web: www.uke.de/imbe<http://www.uke.de/imbe> > -- > > _____________________________________________________________________ > > Universit???tsklinikum Hamburg-Eppendorf; K???rperschaft des ???ffentlichen Rechts; Gerichtsstand: Hamburg | www.uke.de > Vorstandsmitglieder: Prof. Dr. Burkhard G???ke (Vorsitzender), Prof. Dr. Dr. Uwe Koch-Gromus, Joachim Pr???l???, Martina Saurin (komm.) > _____________________________________________________________________ > > SAVE PAPER - THINK BEFORE PRINTING > > [[alternative HTML version deleted]] > >-- Luke Tierney Ralph E. Wareham Professor of Mathematical Sciences University of Iowa Phone: 319-335-3386 Department of Statistics and Fax: 319-335-3017 Actuarial Science 241 Schaeffer Hall email: luke-tierney at uiowa.edu Iowa City, IA 52242 WWW: http://www.stat.uiowa.edu
Thank you, Luke. Unfortunately I don't use Rscript (it is not allowed to run shell scripts on my professional computer). Best regards, Olivier ________________________________ De : Tierney, Luke <luke-tierney at uiowa.edu> Envoy? : mercredi 10 octobre 2018 15:48 ? : Olivier GIVAUDAN Cc : Eik Vettorazzi; r-help at r-project.org Objet : Re: [R] Genuine relative paths with R If you are always running your main script as 'Rscript myscript.R' then your file argument will appear as '--file=myscript.R' in commandArgs(). So you can use something like scriptFile <- function() { pat <- "^--file=" args <- commandArgs() file <- args[grepl(pat, args, args)] normalizePath(sub(pat, "", file)) } Then scriptFile() will give you something to work with. Best, luke On Wed, 10 Oct 2018, Olivier GIVAUDAN wrote:> Dear Eik, > > Thank you for your message. > > Yes, I already got to know this package and this discussion (while I found the 'here' package). But thanks. > > Best regards, > > Olivier > ________________________________ > De : Eik Vettorazzi <E.Vettorazzi at uke.de> > Envoy??? : lundi 8 octobre 2018 11:30 > ??? : Olivier GIVAUDAN; r-help at r-project.org > Objet : Re: [R] Genuine relative paths with R > > Dear Olivier, > you may find the rprojroot package useful, see > https://github.com/r-lib/rprojroot > > and a discussion > > https://gist.github.com/jennybc/362f52446fe1ebc4c49f#file-2014-10-12_stop-working-directory-insanity-md > > Cheers > > Am 06.10.2018 um 13:48 schrieb Olivier GIVAUDAN: >> 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 at 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. >> > > -- > Eik Vettorazzi > > Department of Medical Biometry and Epidemiology > University Medical Center Hamburg-Eppendorf > > Martinistrasse 52 > building W 34 > 20246 Hamburg > > Phone: +49 (0) 40 7410 - 58243 > Fax: +49 (0) 40 7410 - 57790 > Web: www.uke.de/imbe<http://www.uke.de/imbe> > -- > > _____________________________________________________________________ > > Universit???tsklinikum Hamburg-Eppendorf; K???rperschaft des ???ffentlichen Rechts; Gerichtsstand: Hamburg | www.uke.de > Vorstandsmitglieder: Prof. Dr. Burkhard G???ke (Vorsitzender), Prof. Dr. Dr. Uwe Koch-Gromus, Joachim Pr???l???, Martina Saurin (komm.) > _____________________________________________________________________ > > SAVE PAPER - THINK BEFORE PRINTING > > [[alternative HTML version deleted]] > >-- Luke Tierney Ralph E. Wareham Professor of Mathematical Sciences University of Iowa Phone: 319-335-3386 Department of Statistics and Fax: 319-335-3017 Actuarial Science 241 Schaeffer Hall email: luke-tierney at uiowa.edu Iowa City, IA 52242 WWW: http://www.stat.uiowa.edu [[alternative HTML version deleted]]