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]]
Hi Oliver, Interesting question. Can you describe your motivation in a little more detail? That is, why do you what this feature? I ask because to my way of thinking you have to know the path to the script in order to call it in the first place. If calling from R, is setwd("/path/to/dir") source("file.R") really so much worse than source("path/to/dir/file.R") ? If it is worse, why? Similarly if calling from a shell, is cd /path/to/dir Rscript file.R really so much worse than Rscript /path/to/dir/file.R ? Again, if is worse, why? Best, Ista On Sat, Oct 6, 2018 at 8:25 AM Olivier GIVAUDAN <olivier_givaudan at 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 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.
Hi Olivier, Sorry for misspelling your name! Please see inline below. On Sat, Oct 6, 2018 at 9:58 AM Olivier GIVAUDAN <olivier_givaudan at hotmail.com> wrote:> > Hi Ista, > > Thank you for your reply. > > My motivation is described at the 3rd line of my initial message: > "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." > > The main point is simply to avoid any hardcoded path in my R scripts, as you wrote in your examples.My claim is that you cannot avoid hardcoding paths. Even if the feature you requested existed, you would still have to do e.g., source("/path/to/dir/file.R") That is just as much hard-coded as the (only slightly more verbose) cd("/path/to/dir") source("file.R") So, I'm afraid I still don't get it. What is it that you actually want to do that you currently cannot? Best, Ista> > BTW I don't use R with command line, only within RStudio or with the default GUI. > > Best regards, > > Olivier (and not Oliver ) > ________________________________ > De : Ista Zahn <istazahn at gmail.com> > Envoy? : samedi 6 octobre 2018 12:56 > ? : olivier_givaudan at hotmail.com > Cc : r-help at r-project.org > Objet : Re: [R] Genuine relative paths with R > > Hi Oliver, > > Interesting question. Can you describe your motivation in a little > more detail? That is, why do you what this feature? I ask because to > my way of thinking you have to know the path to the script in order to > call it in the first place. If calling from R, is > > setwd("/path/to/dir") > source("file.R") > > really so much worse than > > source("path/to/dir/file.R") > > ? If it is worse, why? > > Similarly if calling from a shell, is > > cd /path/to/dir > Rscript file.R > > really so much worse than > > Rscript /path/to/dir/file.R > > ? Again, if is worse, why? > > Best, > Ista > On Sat, Oct 6, 2018 at 8:25 AM Olivier GIVAUDAN > <olivier_givaudan at 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 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.
On Sat, Oct 6, 2018 at 12:40 PM Ista Zahn <istazahn at gmail.com> wrote:> > Hi Olivier, > > Sorry for misspelling your name! Please see inline below. > > On Sat, Oct 6, 2018 at 9:58 AM Olivier GIVAUDAN > <olivier_givaudan at hotmail.com> wrote: > > > > Hi Ista, > > > > Thank you for your reply. > > > > My motivation is described at the 3rd line of my initial message: > > "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." > > > > The main point is simply to avoid any hardcoded path in my R scripts, as you wrote in your examples. > > My claim is that you cannot avoid hardcoding paths. Even if the > feature you requested existed, you would still have to do e.g., > > source("/path/to/dir/file.R") > > That is just as much hard-coded as the (only slightly more verbose) > > cd("/path/to/dir") > source("file.R") > > So, I'm afraid I still don't get it. What is it that you actually want > to do that you currently cannot?I should have added that my assumption is that you will call setwd("/path/to/dir") source("file.R") interactively, not that you will hard-code this in your script. Best, Ista> > Best, > Ista > > > > BTW I don't use R with command line, only within RStudio or with the default GUI. > > > > Best regards, > > > > Olivier (and not Oliver ) > > ________________________________ > > De : Ista Zahn <istazahn at gmail.com> > > Envoy? : samedi 6 octobre 2018 12:56 > > ? : olivier_givaudan at hotmail.com > > Cc : r-help at r-project.org > > Objet : Re: [R] Genuine relative paths with R > > > > Hi Oliver, > > > > Interesting question. Can you describe your motivation in a little > > more detail? That is, why do you what this feature? I ask because to > > my way of thinking you have to know the path to the script in order to > > call it in the first place. If calling from R, is > > > > setwd("/path/to/dir") > > source("file.R") > > > > really so much worse than > > > > source("path/to/dir/file.R") > > > > ? If it is worse, why? > > > > Similarly if calling from a shell, is > > > > cd /path/to/dir > > Rscript file.R > > > > really so much worse than > > > > Rscript /path/to/dir/file.R > > > > ? Again, if is worse, why? > > > > Best, > > Ista > > On Sat, Oct 6, 2018 at 8:25 AM Olivier GIVAUDAN > > <olivier_givaudan at 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 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.
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 at 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 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.-- Sent from my phone. Please excuse my brevity.
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 at 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 at dcn.davis.ca.us> >Envoy? : samedi 6 octobre 2018 19:31 >? : r-help at r-project.org; Olivier GIVAUDAN; r-help at 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 at 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 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. > >-- >Sent from my phone. Please excuse my brevity.-- Sent from my phone. Please excuse my brevity.
Hi Ista, No worry :) "/path/to/dir" is what I call hardcoded path. And I do want to work with relative paths in code, not interacticely. I (but not only I actually!) need genuine relative paths especially for inputting or outputting files in a location "close" (e.g. parent or child folder) to where the R script is located. I'm looking for the equivalent of the function cd in batch / shell, for instance, or __DIR__ in PHP, or %sysget(SAS_EXECFILEPATH) in SAS, etc. Is it clearer now? Best regards, Olivier ________________________________ De : Ista Zahn <istazahn at gmail.com> Envoy? : samedi 6 octobre 2018 16:40 ? : olivier_givaudan at hotmail.com; r-help at r-project.org Objet : Re: [R] Genuine relative paths with R Hi Olivier, Sorry for misspelling your name! Please see inline below. On Sat, Oct 6, 2018 at 9:58 AM Olivier GIVAUDAN <olivier_givaudan at hotmail.com> wrote:> > Hi Ista, > > Thank you for your reply. > > My motivation is described at the 3rd line of my initial message: > "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." > > The main point is simply to avoid any hardcoded path in my R scripts, as you wrote in your examples.My claim is that you cannot avoid hardcoding paths. Even if the feature you requested existed, you would still have to do e.g., source("/path/to/dir/file.R") That is just as much hard-coded as the (only slightly more verbose) cd("/path/to/dir") source("file.R") So, I'm afraid I still don't get it. What is it that you actually want to do that you currently cannot? Best, Ista> > BTW I don't use R with command line, only within RStudio or with the default GUI. > > Best regards, > > Olivier (and not Oliver ) > ________________________________ > De : Ista Zahn <istazahn at gmail.com> > Envoy? : samedi 6 octobre 2018 12:56 > ? : olivier_givaudan at hotmail.com > Cc : r-help at r-project.org > Objet : Re: [R] Genuine relative paths with R > > Hi Oliver, > > Interesting question. Can you describe your motivation in a little > more detail? That is, why do you what this feature? I ask because to > my way of thinking you have to know the path to the script in order to > call it in the first place. If calling from R, is > > setwd("/path/to/dir") > source("file.R") > > really so much worse than > > source("path/to/dir/file.R") > > ? If it is worse, why? > > Similarly if calling from a shell, is > > cd /path/to/dir > Rscript file.R > > really so much worse than > > Rscript /path/to/dir/file.R > > ? Again, if is worse, why? > > Best, > Ista > On Sat, Oct 6, 2018 at 8:25 AM Olivier GIVAUDAN > <olivier_givaudan at 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 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.[[alternative HTML version deleted]]
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 at dcn.davis.ca.us> Envoy? : samedi 6 octobre 2018 19:31 ? : r-help at r-project.org; Olivier GIVAUDAN; r-help at 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 at 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 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.-- Sent from my phone. Please excuse my brevity. [[alternative HTML version deleted]]
Hi Olivier, Please keep the list copied so others can join our discussion. On Sat, Oct 6, 2018 at 4:39 PM Olivier GIVAUDAN <olivier_givaudan at hotmail.com> wrote:> > Hi Ista, > > No worry :) > > "/path/to/dir" is what I call hardcoded path.Yes, we share the same definition :-)> And I do want to work with relative paths in code, not interacticely.Yes, using relative paths in code is a good idea. We agree about this also!> > I (but not only I actually!) need genuine relative paths especially for inputting or outputting files in a location "close" (e.g. parent or child folder) to where the R script is located.You still have not explained why!> > I'm looking for the equivalent of the function cd in batch / shell, for instance, or __DIR__ in PHP, or %sysget(SAS_EXECFILEPATH) in SAS, etc. > > Is it clearer now?I'm not confused about what you are asking for. I'm only asking for more details about why you think you need it. Again, my claim is that all this feature would do is allow the user of the script to interactively call it with e.g., Rscript /path/to/dir/file.R instead of cd /path/to/dir/file.R Rscript file.R Note that I assume that only relative paths are used in the scripts. The path is specified by the user when running the script, and this cannot be avoided. Thus some "auto setwd to the source file directory" would serve only as a minor convenience that enables my first from above as opposed to my second form. Is this the benefit you are hoping for, or is there some other reason you want this feature? Note that when calling from R you can already to source("/path/to/dir/file.R", chdir = TRUE) Best, Ista> > Best regards, > > Olivier > > ________________________________ > De : Ista Zahn <istazahn at gmail.com> > Envoy? : samedi 6 octobre 2018 16:40 > ? : olivier_givaudan at hotmail.com; r-help at r-project.org > Objet : Re: [R] Genuine relative paths with R > > Hi Olivier, > > Sorry for misspelling your name! Please see inline below. > > On Sat, Oct 6, 2018 at 9:58 AM Olivier GIVAUDAN > <olivier_givaudan at hotmail.com> wrote: > > > > Hi Ista, > > > > Thank you for your reply. > > > > My motivation is described at the 3rd line of my initial message: > > "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." > > > > The main point is simply to avoid any hardcoded path in my R scripts, as you wrote in your examples. > > My claim is that you cannot avoid hardcoding paths. Even if the > feature you requested existed, you would still have to do e.g., > > source("/path/to/dir/file.R") > > That is just as much hard-coded as the (only slightly more verbose) > > cd("/path/to/dir") > source("file.R") > > So, I'm afraid I still don't get it. What is it that you actually want > to do that you currently cannot? > > Best, > Ista > > > > BTW I don't use R with command line, only within RStudio or with the default GUI. > > > > Best regards, > > > > Olivier (and not Oliver ) > > ________________________________ > > De : Ista Zahn <istazahn at gmail.com> > > Envoy? : samedi 6 octobre 2018 12:56 > > ? : olivier_givaudan at hotmail.com > > Cc : r-help at r-project.org > > Objet : Re: [R] Genuine relative paths with R > > > > Hi Oliver, > > > > Interesting question. Can you describe your motivation in a little > > more detail? That is, why do you what this feature? I ask because to > > my way of thinking you have to know the path to the script in order to > > call it in the first place. If calling from R, is > > > > setwd("/path/to/dir") > > source("file.R") > > > > really so much worse than > > > > source("path/to/dir/file.R") > > > > ? If it is worse, why? > > > > Similarly if calling from a shell, is > > > > cd /path/to/dir > > Rscript file.R > > > > really so much worse than > > > > Rscript /path/to/dir/file.R > > > > ? Again, if is worse, why? > > > > Best, > > Ista > > On Sat, Oct 6, 2018 at 8:25 AM Olivier GIVAUDAN > > <olivier_givaudan at 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 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.
On Sun, 7 Oct 2018, Olivier GIVAUDAN wrote:> Hello Denes, > > > Yes, the path to the "root" folder of your project. You seem to have a? > really esoteric context if you want to run an R script without knowing? > its path in the file system. > > I don't have any esoteric context: I'm just looking for the most generic, automatic and reproducible solution. > Of course you always know where an R script is located, nevertheless it doesn't imply you want to manually write its path in this > R script. The issue is to have to hardcode an absolute path: I don't want that. > > >?Because it is extremely rare that someone - who uses R for what it is? > worth and in a manner how R is supposed to be used - actually needs such? > a function. >> First, the fact that it is rare doesn't mean this need is not legitimate > and relevant: it is needed to make R projects fully movable (i.e. > wherever you want). Second why a vast majority of languages does have > this feature and not R? Why is it useful in these languages and not in > R?The R interpreter does not make assumptions about where the code it is running came from. You might be running it by using the source() function, or by using the Rscript program, or by R CMD BATCH, or using eval() on code you pasted together in an R function, or as byte-compiled code loaded from an RData file. That is, there is not always a file in a particular directory even involved in the executing code. You also keep referring to "this feature" being in many languages, though you seem to be mistaken about most of them... in fact, they, too, know NOT where the script is but where the current directory is ($PWD is the same as getwd()) or where the compilation occurred (__FILE__ in C relates to the source code directory that is usually not where the executable is located and may not even exist on the computer it is running on). I have already pointed out that the solution is to let the OS set the current directory. If you want the user to have access to R independent of your code, the easiest way to leave them in Rgui after your code is done is to use save.image() to create a "myApp.RData" file which can be double-clicked [1]. The double-clicking action by default (as defined by the installation of R) causes the operating system to set the current directory to the one containing the file you double-clicked on and then executes the Rgui program. If you don't want the user to interact with your session, you can use the Rscript executable (also mentioned briefly at the bottom of [1]). In both cases, the user has (unknowingly) set the current directory before running your code, and there is no need to encode where the script is or was inside the script. You can also create a windows shortcut to invoke Rscript yourself by bootstrapping the RData file and invoking the R.utils::createWindowsShortcut() [2] function. However, by far the best approach is to teach your users to fish... if you give them an RStudio project directory they can double-click on the .Rproj file to set the current directory and enter the world of R. [1] https://www.r-bloggers.com/look-ma-no-typing-autorunning-code-on-r-startup/ [2] https://cran.r-project.org/web/packages/R.utils/R.utils.pdf End comment.> Best regards, > > Olivier > > _________________________________________________________________________________________________________________________________ > De : D?nes T?th <toth.denes at kogentum.hu> > Envoy? : samedi 6 octobre 2018 23:36 > ? : Olivier GIVAUDAN; Jeff Newmiller; r-help at r-project.org > Objet : Re: [R] Genuine relative paths with R ? > Hi Olivier, > > > On 10/07/2018 01:13 AM, Olivier GIVAUDAN wrote: > > Hi Denes, > > > > Thank you for the possibility you shared: unfortunately it still uses > > one hardcoded absolute path which I want to avoid. > > Yes, the path to the "root" folder of your project. You seem to have a > really esoteric context if you want to run an R script without knowing > its path in the file system. > > > > > I just think that the solutions suggested are too complicated for my > > simple need. > > > > The root cause being that R doesn't seem to have the Windows batch > > equivalent of cd, or bash equivalent of $PWD, or PHP equivalent of __DIR__. > > Hence the workarounds we are discussing. > > > > And finally we go back to my initial question: if such a function > > doesn't exist in R, what are the reasons? > > Because it is extremely rare that someone - who uses R for what it is > worth and in a manner how R is supposed to be used - actually needs such > a function. > > Best, > Denes > > > > > > > Best regards, > > > > Olivier > > ------------------------------------------------------------------------ > > *De :* D?nes T?th <toth.denes at kogentum.hu> > > *Envoy? :* samedi 6 octobre 2018 23:05 > > *?:* Olivier GIVAUDAN; Jeff Newmiller; r-help at r-project.org > > *Objet :* Re: [R] Genuine relative paths with R > > Hi Olivier, > > > > I really think that Ista and Jeff gave you plenty of useful options how > > you can avoid using absolute paths. > > > > One more possibility: you can use `source()` with the chdir = TRUE > > argument (see ?source). If you have a master script which sources other > > files which are located in a fixed hierarchy relative to the location of > > the master script, the only time when you have to use an absolute path > > is when you source your master script, e.g.: > > source("/my/path/to/master.R", chdir = TRUE) > > > > Inside the master script, you can then source the other scripts by > > relative paths, define your 'data' folders relative to the master script > > and let the other scripts use those data paths, etc. > > > > Best, > > Denes > > > > > > On 10/06/2018 11:36 PM, Olivier GIVAUDAN 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 at dcn.davis.ca.us> > >> Envoy??? : samedi 6 octobre 2018 19:31 > >> ??? : r-help at r-project.org; Olivier GIVAUDAN; r-help at 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 at 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 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. > >> > >> -- > >> Sent from my phone. Please excuse my brevity. > >> > >>??????? [[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. > >> > >--------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k ---------------------------------------------------------------------------
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 -- _____________________________________________________________________ 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
Hi Jeff, ?> That is, there is not always a file in a particular directory even involved in the executing code.??? True. I'm only asking in the case where some R code is run from an R file. This function 'MyOwnPath()' (say) should only work (i.e. return a useful result) in this case.? ?> The R interpreter does not make assumptions about where the code it is running came from.?? That's precisely the reproach I have in case the code comes from a file.? ?> You also keep referring to "this feature" being in many languages, though you seem to be mistaken about most of them...?? 'Most of them' is largely exaggerated (plus I corrected myself): I only mixed between the 'cd' variable / command in Windows and Linux shells (you would certainly agree it's a bit vicious).? ?> $PWD is the same as getwd()?? Wrong. As I already said, if I create an R script located somewhere on my computer with the only line 'getwd()' and run this script, I won't get the directory of this R script, contrary to a shell file with the only line '$PWD'.? ?> __FILE__ in C relates to the source code directory that is usually not where the executable is ?located and may not even exist on the computer it is running on?? Yes, and? What is the issue here? So '__FILE__' does the job. Invoking '__FILE__' in a .C file (I never said in its related executable) will return the absolute path of this C source file.? '__DIR__' in PHP also does the job. No to forget the good old VBA (Excel in this case) 'ActiveWorkbook.Path'.? Thank you for the 2 references you provided. However it seems that some manual settings are still required to be able to use Rscript. But I like the solution of the .RData file: I simply created an empty .RData file at the root of my project and by double-clicking on this file (as you said), the R GUI opens and 'getwd()' returns the path of this .RData file. It seems to be a good alternative to .Rproj file to be opened with RStudio. ?> However, by far the best approach is to teach your users to fish... if you give them an RStudio project directory they can double-click on the .Rproj ?file to set the current directory and enter the world of R.??? Yes, using an .Rproj file to be opened with RStudio also seems to me to be a reasonable solution (the one I chose until now), although it is still a workaround and requires RStudio.? Actually in this case, the package 'here'? I previously mentioned is useless to get the current working directory of the project as 'When a project is opened within RStudio the following actions are taken: [...] The current working directory is set to the project directory.', as stated on this page: https://support.rstudio.com/hc/en-us/articles/200526207-Using-Projects. So 'getwd()' returns exactly the same as 'here()' does. Best regards,? ? Olivier ________________________________ De : Jeff Newmiller <jdnewmil at dcn.davis.ca.us> Envoy? : dimanche 7 octobre 2018 20:48 ? : Olivier GIVAUDAN Cc : D?nes T?th; r-help at r-project.org Objet : RE: [R] Genuine relative paths with R On Sun, 7 Oct 2018, Olivier GIVAUDAN wrote:> Hello Denes, > > > Yes, the path to the "root" folder of your project. You seem to have a > really esoteric context if you want to run an R script without knowing > its path in the file system. > > I don't have any esoteric context: I'm just looking for the most generic, automatic and reproducible solution. > Of course you always know where an R script is located, nevertheless it doesn't imply you want to manually write its path in this > R script. The issue is to have to hardcode an absolute path: I don't want that. > > > Because it is extremely rare that someone - who uses R for what it is > worth and in a manner how R is supposed to be used - actually needs such > a function. >> First, the fact that it is rare doesn't mean this need is not legitimate > and relevant: it is needed to make R projects fully movable (i.e. > wherever you want). Second why a vast majority of languages does have > this feature and not R? Why is it useful in these languages and not in > R?The R interpreter does not make assumptions about where the code it is running came from. You might be running it by using the source() function, or by using the Rscript program, or by R CMD BATCH, or using eval() on code you pasted together in an R function, or as byte-compiled code loaded from an RData file. That is, there is not always a file in a particular directory even involved in the executing code. You also keep referring to "this feature" being in many languages, though you seem to be mistaken about most of them... in fact, they, too, know NOT where the script is but where the current directory is ($PWD is the same as getwd()) or where the compilation occurred (__FILE__ in C relates to the source code directory that is usually not where the executable is located and may not even exist on the computer it is running on). I have already pointed out that the solution is to let the OS set the current directory. If you want the user to have access to R independent of your code, the easiest way to leave them in Rgui after your code is done is to use save.image() to create a "myApp.RData" file which can be double-clicked [1]. The double-clicking action by default (as defined by the installation of R) causes the operating system to set the current directory to the one containing the file you double-clicked on and then executes the Rgui program. If you don't want the user to interact with your session, you can use the Rscript executable (also mentioned briefly at the bottom of [1]). In both cases, the user has (unknowingly) set the current directory before running your code, and there is no need to encode where the script is or was inside the script. You can also create a windows shortcut to invoke Rscript yourself by bootstrapping the RData file and invoking the R.utils::createWindowsShortcut() [2] function. However, by far the best approach is to teach your users to fish... if you give them an RStudio project directory they can double-click on the .Rproj file to set the current directory and enter the world of R. [1] https://www.r-bloggers.com/look-ma-no-typing-autorunning-code-on-r-startup/ [2] https://cran.r-project.org/web/packages/R.utils/R.utils.pdf End comment.> Best regards, > > Olivier > > _________________________________________________________________________________________________________________________________ > De : D?nes T?th <toth.denes at kogentum.hu> > Envoy? : samedi 6 octobre 2018 23:36 > ? : Olivier GIVAUDAN; Jeff Newmiller; r-help at r-project.org > Objet : Re: [R] Genuine relative paths with R > Hi Olivier, > > > On 10/07/2018 01:13 AM, Olivier GIVAUDAN wrote: > > Hi Denes, > > > > Thank you for the possibility you shared: unfortunately it still uses > > one hardcoded absolute path which I want to avoid. > > Yes, the path to the "root" folder of your project. You seem to have a > really esoteric context if you want to run an R script without knowing > its path in the file system. > > > > > I just think that the solutions suggested are too complicated for my > > simple need. > > > > The root cause being that R doesn't seem to have the Windows batch > > equivalent of cd, or bash equivalent of $PWD, or PHP equivalent of __DIR__. > > Hence the workarounds we are discussing. > > > > And finally we go back to my initial question: if such a function > > doesn't exist in R, what are the reasons? > > Because it is extremely rare that someone - who uses R for what it is > worth and in a manner how R is supposed to be used - actually needs such > a function. > > Best, > Denes > > > > > > > Best regards, > > > > Olivier > > ------------------------------------------------------------------------ > > *De :* D?nes T?th <toth.denes at kogentum.hu> > > *Envoy? :* samedi 6 octobre 2018 23:05 > > *?:* Olivier GIVAUDAN; Jeff Newmiller; r-help at r-project.org > > *Objet :* Re: [R] Genuine relative paths with R > > Hi Olivier, > > > > I really think that Ista and Jeff gave you plenty of useful options how > > you can avoid using absolute paths. > > > > One more possibility: you can use `source()` with the chdir = TRUE > > argument (see ?source). If you have a master script which sources other > > files which are located in a fixed hierarchy relative to the location of > > the master script, the only time when you have to use an absolute path > > is when you source your master script, e.g.: > > source("/my/path/to/master.R", chdir = TRUE) > > > > Inside the master script, you can then source the other scripts by > > relative paths, define your 'data' folders relative to the master script > > and let the other scripts use those data paths, etc. > > > > Best, > > Denes > > > > > > On 10/06/2018 11:36 PM, Olivier GIVAUDAN 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 at dcn.davis.ca.us> > >> Envoy??? : samedi 6 octobre 2018 19:31 > >> ??? : r-help at r-project.org; Olivier GIVAUDAN; r-help at 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 at 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 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. > >> > >> -- > >> Sent from my phone. Please excuse my brevity. > >> > >> [[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. > >> > >--------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- [[alternative HTML version deleted]]
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]]