Genuine relative paths with R

classic Classic list List threaded Threaded
52 messages Options
123
Reply | Threaded
Open this post in threaded view
|

Re: Genuine relative paths with R

R help mailing list-2
I am curious about why it is desirable for a script to know where in the
file system it is.  Is it because you have a set of scripts invoking each
other?  If so, the best route it to convert the scripts into functions and
put them into a package.  Functions do know which package they live in and
they "prefer" to call other functions in their own package (or other
packages which they have declared).

Or is there some other reason?


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Wed, Oct 10, 2018 at 8:18 AM, Olivier GIVAUDAN <
[hidden email]> wrote:

> Hi Duncan,
>
> Yes, if you need to display the content of $PWD you obviously need to type
> 'echo' before this variable.
>
> It prints the user's working directory if run from a terminal but if run
> from a bash file it prints the working directory of the script.
> At least for me (I am running on the last version of Ubuntu)...
>
> Best regards,
>
> Olivier
> ________________________________
> De : Duncan Murdoch <[hidden email]>
> Envoyé : mercredi 10 octobre 2018 14:51
> À : Olivier GIVAUDAN; Jeff Newmiller
> Cc : [hidden email]
> Objet : Re: [R] Genuine relative paths with R
>
> On 10/10/2018 10:37 AM, Olivier GIVAUDAN wrote:
> > 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'.​
>
> What system are you talking about?  On Unix-alikes, you'd need an "echo"
> ahead of that, and it would print the user's working directory, not the
> working directory of the script.
>
> Duncan Murdoch
>
> > ​
> >> __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 <[hidden email]>
> > Envoyé : dimanche 7 octobre 2018 20:48
> > À : Olivier GIVAUDAN
> > Cc : Dénes Tóth; [hidden email]
> > 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 <[hidden email]>
> >> Envoy? : samedi 6 octobre 2018 23:36
> >> ? : Olivier GIVAUDAN; Jeff Newmiller; [hidden email]
> >> 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 <[hidden email]>
> >>> *Envoy? :* samedi 6 octobre 2018 23:05
> >>> *?:* Olivier GIVAUDAN; Jeff Newmiller; [hidden email]
> >>> *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 <[hidden email]>
> >>>> Envoy??? : samedi 6 octobre 2018 19:31
> >>>> ??? : [hidden email]; Olivier GIVAUDAN; [hidden email]
> >>>> 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 <
> [hidden email]> 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]]
> >>>>>
> >>>>> ______________________________________________
> >>>>> [hidden email] 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]]
> >>>>
> >>>>
> >>>>
> >>>> ______________________________________________
> >>>> [hidden email] 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:<[hidden email]>        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]]
> >
> > ______________________________________________
> > [hidden email] 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]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Genuine relative paths with R

Tierney, Luke
In reply to this post by Olivier GIVAUDAN
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 <[hidden email]>
> Envoy??? : lundi 8 octobre 2018 11:30
> ??? : Olivier GIVAUDAN; [hidden email]
> 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]]
>>
>> ______________________________________________
>> [hidden email] 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:   [hidden email]
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Genuine relative paths with R

Olivier GIVAUDAN
In reply to this post by Duncan Murdoch-2
Well, no idea...

I just created a file 'TestPWD', made it executable, inserted in it these lines and moved the file in various places:

#!/bin/bash

echo $PWD
read -p ""
________________________________
De : Duncan Murdoch <[hidden email]>
Envoyé : mercredi 10 octobre 2018 15:20
À : Olivier GIVAUDAN; Jeff Newmiller
Cc : [hidden email]
Objet : Re: [R] Genuine relative paths with R

On 10/10/2018 11:18 AM, Olivier GIVAUDAN wrote:
> Hi Duncan,
>
> Yes, if you need to display the content of $PWD you obviously need to
> type 'echo' before this variable.
>
> It prints the user's working directory if run from a terminal but if run
> from a bash file it prints the working directory of the script.
> At least for me (I am running on the last version of Ubuntu)...

Not for me.  Always prints the user's working directory.

Duncan Murdoch


>
> Best regards,
>
> Olivier
> ------------------------------------------------------------------------
> *De :* Duncan Murdoch <[hidden email]>
> *Envoyé :* mercredi 10 octobre 2018 14:51
> *À :* Olivier GIVAUDAN; Jeff Newmiller
> *Cc :* [hidden email]
> *Objet :* Re: [R] Genuine relative paths with R
> On 10/10/2018 10:37 AM, Olivier GIVAUDAN wrote:
>> 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'.​
>
> What system are you talking about?  On Unix-alikes, you'd need an "echo"
> ahead of that, and it would print the user's working directory, not the
> working directory of the script.
>
> Duncan Murdoch
>
>> ​
>>> __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 <[hidden email]>
>> Envoyé : dimanche 7 octobre 2018 20:48
>> À : Olivier GIVAUDAN
>> Cc : Dénes Tóth; [hidden email]
>> 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 <[hidden email]>
>>> Envoy? : samedi 6 octobre 2018 23:36
>>> ? : Olivier GIVAUDAN; Jeff Newmiller; [hidden email]
>>> 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 <[hidden email]>
>>>> *Envoy? :* samedi 6 octobre 2018 23:05
>>>> *?:* Olivier GIVAUDAN; Jeff Newmiller; [hidden email]
>>>> *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 <[hidden email]>
>>>>> Envoy??? : samedi 6 octobre 2018 19:31
>>>>> ??? : [hidden email]; Olivier GIVAUDAN; [hidden email]
>>>>> 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 <[hidden email]> 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]]
>>>>>>
>>>>>> ______________________________________________
>>>>>> [hidden email] 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]]
>>>>>
>>>>>
>>>>>
>>>>> ______________________________________________
>>>>> [hidden email] 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:<[hidden email]>        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]]
>>
>> ______________________________________________
>> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Genuine relative paths with R

Olivier GIVAUDAN
In reply to this post by R help mailing list-2
To be able to change easily, cleanly and automatically the working directory and thus to work with relative paths in the close neighbourhood of your R files.
________________________________
De : William Dunlap <[hidden email]>
Envoyé : mercredi 10 octobre 2018 15:25
À : Olivier GIVAUDAN
Cc : Duncan Murdoch; Jeff Newmiller; [hidden email]
Objet : Re: [R] Genuine relative paths with R

I am curious about why it is desirable for a script to know where in the file system it is.  Is it because you have a set of scripts invoking each other?  If so, the best route it to convert the scripts into functions and put them into a package.  Functions do know which package they live in and they "prefer" to call other functions in their own package (or other packages which they have declared).

Or is there some other reason?


Bill Dunlap
TIBCO Software
wdunlap tibco.com<http://tibco.com>

On Wed, Oct 10, 2018 at 8:18 AM, Olivier GIVAUDAN <[hidden email]<mailto:[hidden email]>> wrote:
Hi Duncan,

Yes, if you need to display the content of $PWD you obviously need to type 'echo' before this variable.

It prints the user's working directory if run from a terminal but if run from a bash file it prints the working directory of the script.
At least for me (I am running on the last version of Ubuntu)...

Best regards,

Olivier
________________________________
De : Duncan Murdoch <[hidden email]<mailto:[hidden email]>>
Envoyé : mercredi 10 octobre 2018 14:51
À : Olivier GIVAUDAN; Jeff Newmiller
Cc : [hidden email]<mailto:[hidden email]>
Objet : Re: [R] Genuine relative paths with R

On 10/10/2018 10:37 AM, Olivier GIVAUDAN wrote:

> 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'.​

What system are you talking about?  On Unix-alikes, you'd need an "echo"
ahead of that, and it would print the user's working directory, not the
working directory of the script.

Duncan Murdoch

> ​
>> __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 <[hidden email]<mailto:[hidden email]>>
> Envoyé : dimanche 7 octobre 2018 20:48
> À : Olivier GIVAUDAN
> Cc : Dénes Tóth; [hidden email]<mailto:[hidden email]>
> 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 <[hidden email]<mailto:[hidden email]>>
>> Envoy? : samedi 6 octobre 2018 23:36
>> ? : Olivier GIVAUDAN; Jeff Newmiller; [hidden email]<mailto:[hidden email]>
>> 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 <[hidden email]<mailto:[hidden email]>>
>>> *Envoy? :* samedi 6 octobre 2018 23:05
>>> *?:* Olivier GIVAUDAN; Jeff Newmiller; [hidden email]<mailto:[hidden email]>
>>> *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 <[hidden email]<mailto:[hidden email]>>
>>>> Envoy??? : samedi 6 octobre 2018 19:31
>>>> ??? : [hidden email]<mailto:[hidden email]>; Olivier GIVAUDAN; [hidden email]<mailto:[hidden email]>
>>>> 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 <[hidden email]<mailto:[hidden email]>> 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]]
>>>>>
>>>>> ______________________________________________
>>>>> [hidden email]<mailto:[hidden email]> 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]]
>>>>
>>>>
>>>>
>>>> ______________________________________________
>>>> [hidden email]<mailto:[hidden email]> 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:<[hidden email]<mailto:[hidden email]>>        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]]
>
> ______________________________________________
> [hidden email]<mailto:[hidden email]> 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]]

______________________________________________
[hidden email]<mailto:[hidden email]> 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Genuine relative paths with R

Olivier GIVAUDAN
In reply to this post by Tierney, Luke
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 <[hidden email]>
Envoy� : mercredi 10 octobre 2018 15:48
� : Olivier GIVAUDAN
Cc : Eik Vettorazzi; [hidden email]
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 <[hidden email]>
> Envoy??? : lundi 8 octobre 2018 11:30
> ??? : Olivier GIVAUDAN; [hidden email]
> 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]]
>>
>> ______________________________________________
>> [hidden email] 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:   [hidden email]
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Genuine relative paths with R

Duncan Murdoch-2
In reply to this post by Olivier GIVAUDAN
On 10/10/2018 3:51 PM, Olivier GIVAUDAN wrote:
> Well, no idea...
>
> I just created a file 'TestPWD', made it executable, inserted in it
> these lines and moved the file in various places:
>
> #!/bin/bash
>
> echo $PWD
> read -p ""

When I execute that, it prints my working directory.  Doesn't matter
where the TestPWD file is.  If I put it in directory "foo", and change
to foo's parent, then

   foo/TestPWD

prints the name of the parent directory, not the foo directory.  If I
then add foo to the PATH, and run

   TestPWD

I get the same thing.  I find it unbelievable that you are seeing
anything different from that on a Unix-alike.

Duncan Muroch


> ------------------------------------------------------------------------
> *De :* Duncan Murdoch <[hidden email]>
> *Envoyé :* mercredi 10 octobre 2018 15:20
> *À :* Olivier GIVAUDAN; Jeff Newmiller
> *Cc :* [hidden email]
> *Objet :* Re: [R] Genuine relative paths with R
> On 10/10/2018 11:18 AM, Olivier GIVAUDAN wrote:
>> Hi Duncan,
>>
>> Yes, if you need to display the content of $PWD you obviously need to
>> type 'echo' before this variable.
>>
>> It prints the user's working directory if run from a terminal but if run
>> from a bash file it prints the working directory of the script.
>> At least for me (I am running on the last version of Ubuntu)...
>
> Not for me.  Always prints the user's working directory.
>
> Duncan Murdoch
>
>
>>
>> Best regards,
>>
>> Olivier
>> ------------------------------------------------------------------------
>> *De :* Duncan Murdoch <[hidden email]>
>> *Envoyé :* mercredi 10 octobre 2018 14:51
>> *À :* Olivier GIVAUDAN; Jeff Newmiller
>> *Cc :* [hidden email]
>> *Objet :* Re: [R] Genuine relative paths with R
>> On 10/10/2018 10:37 AM, Olivier GIVAUDAN wrote:
>>> 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'.​
>>
>> What system are you talking about?  On Unix-alikes, you'd need an "echo"
>> ahead of that, and it would print the user's working directory, not the
>> working directory of the script.
>>
>> Duncan Murdoch
>>
>>> ​
>>>> __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 <[hidden email]>
>>> Envoyé : dimanche 7 octobre 2018 20:48
>>> À : Olivier GIVAUDAN
>>> Cc : Dénes Tóth; [hidden email]
>>> 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 <[hidden email]>
>>>> Envoy? : samedi 6 octobre 2018 23:36
>>>> ? : Olivier GIVAUDAN; Jeff Newmiller; [hidden email]
>>>> 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 <[hidden email]>
>>>>> *Envoy? :* samedi 6 octobre 2018 23:05
>>>>> *?:* Olivier GIVAUDAN; Jeff Newmiller; [hidden email]
>>>>> *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 <[hidden email]>
>>>>>> Envoy??? : samedi 6 octobre 2018 19:31
>>>>>> ??? : [hidden email]; Olivier GIVAUDAN; [hidden email]
>>>>>> 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 <[hidden email]> 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]]
>>>>>>>
>>>>>>> ______________________________________________
>>>>>>> [hidden email] 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]]
>>>>>>
>>>>>>
>>>>>>
>>>>>> ______________________________________________
>>>>>> [hidden email] 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:<[hidden email]>        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]]
>>>
>>> ______________________________________________
>>> [hidden email] 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.
>>>
>>
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Genuine relative paths with R

Bert Gunter-2
In reply to this post by Olivier GIVAUDAN
I haven't followed this discussion closely, so this may be offbase, but in
response to your point 2., note that you can set the working directory via
setwd() in your .Rprofile file. Of course, users can always determine the
working directory via invoking the getwd() function, so I'm not sure what
you mean here. However, as I said, if my comments are useless, please
ignore and do not waste time responding.

Cheers,
Bert



On Wed, Oct 10, 2018 at 6:53 AM Olivier GIVAUDAN <
[hidden email]> wrote:

> Hi Gabor,
> ​
>
>   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.​
>
> Best regards,​
> ​
> Olivier
> ________________________________
> De : Gabor Grothendieck <[hidden email]>
> Envoyé : samedi 6 octobre 2018 23:33
> À : [hidden email]; [hidden email]
> 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
> machines.
>
> 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
> <[hidden email]> 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]]
> >
> > ______________________________________________
> > [hidden email] 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.
>
>
>
> --
> Statistics & Software Consulting
> GKX Group, GKX Associates Inc.
> tel: 1-877-GKX-GROUP
> email: ggrothendieck at gmail.com
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Genuine relative paths with R

Olivier GIVAUDAN
In reply to this post by Duncan Murdoch-2
Why are you not simply double-clicking on 'TestPWD' and choosing to execute the file (don't add anything)?
Are you executing the file from a terminal?
________________________________
De : Duncan Murdoch <[hidden email]>
Envoyé : mercredi 10 octobre 2018 20:17
À : Olivier GIVAUDAN; Jeff Newmiller
Cc : [hidden email]
Objet : Re: [R] Genuine relative paths with R

On 10/10/2018 3:51 PM, Olivier GIVAUDAN wrote:
> Well, no idea...
>
> I just created a file 'TestPWD', made it executable, inserted in it
> these lines and moved the file in various places:
>
> #!/bin/bash
>
> echo $PWD
> read -p ""

When I execute that, it prints my working directory.  Doesn't matter
where the TestPWD file is.  If I put it in directory "foo", and change
to foo's parent, then

   foo/TestPWD

prints the name of the parent directory, not the foo directory.  If I
then add foo to the PATH, and run

   TestPWD

I get the same thing.  I find it unbelievable that you are seeing
anything different from that on a Unix-alike.

Duncan Muroch


> ------------------------------------------------------------------------
> *De :* Duncan Murdoch <[hidden email]>
> *Envoyé :* mercredi 10 octobre 2018 15:20
> *À :* Olivier GIVAUDAN; Jeff Newmiller
> *Cc :* [hidden email]
> *Objet :* Re: [R] Genuine relative paths with R
> On 10/10/2018 11:18 AM, Olivier GIVAUDAN wrote:
>> Hi Duncan,
>>
>> Yes, if you need to display the content of $PWD you obviously need to
>> type 'echo' before this variable.
>>
>> It prints the user's working directory if run from a terminal but if run
>> from a bash file it prints the working directory of the script.
>> At least for me (I am running on the last version of Ubuntu)...
>
> Not for me.  Always prints the user's working directory.
>
> Duncan Murdoch
>
>
>>
>> Best regards,
>>
>> Olivier
>> ------------------------------------------------------------------------
>> *De :* Duncan Murdoch <[hidden email]>
>> *Envoyé :* mercredi 10 octobre 2018 14:51
>> *À :* Olivier GIVAUDAN; Jeff Newmiller
>> *Cc :* [hidden email]
>> *Objet :* Re: [R] Genuine relative paths with R
>> On 10/10/2018 10:37 AM, Olivier GIVAUDAN wrote:
>>> 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'.​
>>
>> What system are you talking about?  On Unix-alikes, you'd need an "echo"
>> ahead of that, and it would print the user's working directory, not the
>> working directory of the script.
>>
>> Duncan Murdoch
>>
>>> ​
>>>> __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 <[hidden email]>
>>> Envoyé : dimanche 7 octobre 2018 20:48
>>> À : Olivier GIVAUDAN
>>> Cc : Dénes Tóth; [hidden email]
>>> 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 <[hidden email]>
>>>> Envoy? : samedi 6 octobre 2018 23:36
>>>> ? : Olivier GIVAUDAN; Jeff Newmiller; [hidden email]
>>>> 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 <[hidden email]>
>>>>> *Envoy? :* samedi 6 octobre 2018 23:05
>>>>> *?:* Olivier GIVAUDAN; Jeff Newmiller; [hidden email]
>>>>> *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 <[hidden email]>
>>>>>> Envoy??? : samedi 6 octobre 2018 19:31
>>>>>> ??? : [hidden email]; Olivier GIVAUDAN; [hidden email]
>>>>>> 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 <[hidden email]> 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]]
>>>>>>>
>>>>>>> ______________________________________________
>>>>>>> [hidden email] 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]]
>>>>>>
>>>>>>
>>>>>>
>>>>>> ______________________________________________
>>>>>> [hidden email] 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:<[hidden email]>        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]]
>>>
>>> ______________________________________________
>>> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Genuine relative paths with R

Duncan Murdoch-2
On 10/10/2018 4:42 PM, Olivier GIVAUDAN wrote:
> Why are you not simply double-clicking on 'TestPWD' and choosing to
> execute the file (don't add anything)?
> Are you executing the file from a terminal?

Yes, I was executing the file from my terminal.  Otherwise I really have
no idea what the "current directory" is in the Finder.   (I'm on a Mac.
I just tried the click method; it printed my home directory, not the
directory of the script.)

I don't know the name of your visual front end, but you are displaying
the working directory that it sets when you click on TestPWD.  That will
be different from the working directory that your user sees in the Terminal.

You can see what I saw if you run TestPWD from the Terminal.  It will
print the current working directory, not the one where TestPWD happens
to live.

If you want to do the same sort of thing in R, you could set up a script
that calls R, and execute that in the way you executed TestPWD.  But in
another message you said you aren't allowed to do that, so I think your
best solution is the one offered by Bill Dunlap:  organize your files as
an R package.  If you name your package "Olivier", then you can find all
the files in it under the directory returned by

   system.file(".", package = "Olivier")

The package system is designed for R code, but you can put arbitrary
files into a package:  just store them under the "inst" directory in
your source.  When the package is installed, those files will be moved
up one level, i.e.

Olivier/inst/foo

will become

   system.file("foo", package = "Olivier")

Duncan Murdoch

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Genuine relative paths with R

Olivier GIVAUDAN
In reply to this post by Olivier GIVAUDAN
Actually there is a difference between:

  1.  Directly executing the file by the user by double clicking on the "physical" file (you can only do that from the directory where the file is located), without going through any terminal, and
  2.  Executing this file "remotely" by calling it through a terminal for instance.

________________________________
De : R-help <[hidden email]> de la part de Olivier GIVAUDAN <[hidden email]>
Envoyé : mercredi 10 octobre 2018 20:42
À : Duncan Murdoch; Jeff Newmiller
Cc : [hidden email]
Objet : Re: [R] Genuine relative paths with R

Why are you not simply double-clicking on 'TestPWD' and choosing to execute the file (don't add anything)?
Are you executing the file from a terminal?
________________________________
De : Duncan Murdoch <[hidden email]>
Envoyé : mercredi 10 octobre 2018 20:17
À : Olivier GIVAUDAN; Jeff Newmiller
Cc : [hidden email]
Objet : Re: [R] Genuine relative paths with R

On 10/10/2018 3:51 PM, Olivier GIVAUDAN wrote:
> Well, no idea...
>
> I just created a file 'TestPWD', made it executable, inserted in it
> these lines and moved the file in various places:
>
> #!/bin/bash
>
> echo $PWD
> read -p ""

When I execute that, it prints my working directory.  Doesn't matter
where the TestPWD file is.  If I put it in directory "foo", and change
to foo's parent, then

   foo/TestPWD

prints the name of the parent directory, not the foo directory.  If I
then add foo to the PATH, and run

   TestPWD

I get the same thing.  I find it unbelievable that you are seeing
anything different from that on a Unix-alike.

Duncan Muroch


> ------------------------------------------------------------------------
> *De :* Duncan Murdoch <[hidden email]>
> *Envoyé :* mercredi 10 octobre 2018 15:20
> *À :* Olivier GIVAUDAN; Jeff Newmiller
> *Cc :* [hidden email]
> *Objet :* Re: [R] Genuine relative paths with R
> On 10/10/2018 11:18 AM, Olivier GIVAUDAN wrote:
>> Hi Duncan,
>>
>> Yes, if you need to display the content of $PWD you obviously need to
>> type 'echo' before this variable.
>>
>> It prints the user's working directory if run from a terminal but if run
>> from a bash file it prints the working directory of the script.
>> At least for me (I am running on the last version of Ubuntu)...
>
> Not for me.  Always prints the user's working directory.
>
> Duncan Murdoch
>
>
>>
>> Best regards,
>>
>> Olivier
>> ------------------------------------------------------------------------
>> *De :* Duncan Murdoch <[hidden email]>
>> *Envoyé :* mercredi 10 octobre 2018 14:51
>> *À :* Olivier GIVAUDAN; Jeff Newmiller
>> *Cc :* [hidden email]
>> *Objet :* Re: [R] Genuine relative paths with R
>> On 10/10/2018 10:37 AM, Olivier GIVAUDAN wrote:
>>> 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'.​
>>
>> What system are you talking about?  On Unix-alikes, you'd need an "echo"
>> ahead of that, and it would print the user's working directory, not the
>> working directory of the script.
>>
>> Duncan Murdoch
>>
>>> ​
>>>> __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 <[hidden email]>
>>> Envoyé : dimanche 7 octobre 2018 20:48
>>> À : Olivier GIVAUDAN
>>> Cc : Dénes Tóth; [hidden email]
>>> 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 <[hidden email]>
>>>> Envoy? : samedi 6 octobre 2018 23:36
>>>> ? : Olivier GIVAUDAN; Jeff Newmiller; [hidden email]
>>>> 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 <[hidden email]>
>>>>> *Envoy? :* samedi 6 octobre 2018 23:05
>>>>> *?:* Olivier GIVAUDAN; Jeff Newmiller; [hidden email]
>>>>> *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 <[hidden email]>
>>>>>> Envoy??? : samedi 6 octobre 2018 19:31
>>>>>> ??? : [hidden email]; Olivier GIVAUDAN; [hidden email]
>>>>>> 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 <[hidden email]> 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]]
>>>>>>>
>>>>>>> ______________________________________________
>>>>>>> [hidden email] 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]]
>>>>>>
>>>>>>
>>>>>>
>>>>>> ______________________________________________
>>>>>> [hidden email] 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:<[hidden email]>        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]]
>>>
>>> ______________________________________________
>>> [hidden email] 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]]

______________________________________________
[hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Genuine relative paths with R

Olivier GIVAUDAN
In reply to this post by Duncan Murdoch-2
I do not want to use the terminal, just double clicks (i.e. the simplest, automatic, non-manual way, without having to write a line / command).
Therefore everything should happen outside any terminal. The user won't use a terminal.

I don't have a Mac and I'm not familiar with this OS, sorry.
But I'm really surprised the click method gives different results than on Linux and Windows.
I know the click method worked both on Linux (Ubuntu latest version) and Windows (10).

Yes, I executed my file from a terminal and got obviously the same result as you (that's reassuring).

Come on guys, creating a package... It's like using a hammer to kill a fly...
________________________________
De : Duncan Murdoch <[hidden email]>
Envoy� : mercredi 10 octobre 2018 20:54
� : Olivier GIVAUDAN; Jeff Newmiller
Cc : [hidden email]
Objet : Re: [R] Genuine relative paths with R

On 10/10/2018 4:42 PM, Olivier GIVAUDAN wrote:
> Why are you not simply double-clicking on 'TestPWD' and choosing to
> execute the file (don't add anything)?
> Are you executing the file from a terminal?

Yes, I was executing the file from my terminal.  Otherwise I really have
no idea what the "current directory" is in the Finder.   (I'm on a Mac.
I just tried the click method; it printed my home directory, not the
directory of the script.)

I don't know the name of your visual front end, but you are displaying
the working directory that it sets when you click on TestPWD.  That will
be different from the working directory that your user sees in the Terminal.

You can see what I saw if you run TestPWD from the Terminal.  It will
print the current working directory, not the one where TestPWD happens
to live.

If you want to do the same sort of thing in R, you could set up a script
that calls R, and execute that in the way you executed TestPWD.  But in
another message you said you aren't allowed to do that, so I think your
best solution is the one offered by Bill Dunlap:  organize your files as
an R package.  If you name your package "Olivier", then you can find all
the files in it under the directory returned by

   system.file(".", package = "Olivier")

The package system is designed for R code, but you can put arbitrary
files into a package:  just store them under the "inst" directory in
your source.  When the package is installed, those files will be moved
up one level, i.e.

Olivier/inst/foo

will become

   system.file("foo", package = "Olivier")

Duncan Murdoch

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Genuine relative paths with R

Duncan Murdoch-2
On 10/10/2018 5:31 PM, Olivier GIVAUDAN wrote:

> I do not want to use the terminal, just double clicks (i.e. the
> simplest, automatic, non-manual way, without having to write a line /
> command).
> Therefore everything should happen outside any terminal. The user won't
> use a terminal.
>
> I don't have a Mac and I'm not familiar with this OS, sorry.
> But I'm really surprised the click method gives different results than
> on Linux and Windows.
> I know the click method worked both on Linux (Ubuntu latest version) and
> Windows (10).
>
> Yes, I executed my file from a terminal and got obviously the same
> result as you (that's reassuring).
>
> Come on guys, creating a package... It's like using a hammer to kill a
> fly...

It's a simple operation to create a package in RStudio.  Not quite a
single click, but just a few.

In plain R, it's just a little more work using package.skeleton().

Really, if you are distributing R code, you should do it in the standard
way, not invent your own.

Duncan Murdoch

> ------------------------------------------------------------------------
> *De :* Duncan Murdoch <[hidden email]>
> *Envoyé :* mercredi 10 octobre 2018 20:54
> *À :* Olivier GIVAUDAN; Jeff Newmiller
> *Cc :* [hidden email]
> *Objet :* Re: [R] Genuine relative paths with R
> On 10/10/2018 4:42 PM, Olivier GIVAUDAN wrote:
>> Why are you not simply double-clicking on 'TestPWD' and choosing to
>> execute the file (don't add anything)?
>> Are you executing the file from a terminal?
>
> Yes, I was executing the file from my terminal.  Otherwise I really have
> no idea what the "current directory" is in the Finder.   (I'm on a Mac.
> I just tried the click method; it printed my home directory, not the
> directory of the script.)
>
> I don't know the name of your visual front end, but you are displaying
> the working directory that it sets when you click on TestPWD.  That will
> be different from the working directory that your user sees in the Terminal.
>
> You can see what I saw if you run TestPWD from the Terminal.  It will
> print the current working directory, not the one where TestPWD happens
> to live.
>
> If you want to do the same sort of thing in R, you could set up a script
> that calls R, and execute that in the way you executed TestPWD.  But in
> another message you said you aren't allowed to do that, so I think your
> best solution is the one offered by Bill Dunlap:  organize your files as
> an R package.  If you name your package "Olivier", then you can find all
> the files in it under the directory returned by
>
>     system.file(".", package = "Olivier")
>
> The package system is designed for R code, but you can put arbitrary
> files into a package:  just store them under the "inst" directory in
> your source.  When the package is installed, those files will be moved
> up one level, i.e.
>
> Olivier/inst/foo
>
> will become
>
>     system.file("foo", package = "Olivier")
>
> Duncan Murdoch

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Genuine relative paths with R

Olivier GIVAUDAN
I'm not sure I'm "inventing my own way" of distributing R code... And I distribute it to a very limited audience.
Anyway, why not "inventing a new way" if it's more efficient than the standard one (I'm talking now in theory)?
________________________________
De : Duncan Murdoch <[hidden email]>
Envoy� : mercredi 10 octobre 2018 21:39
� : Olivier GIVAUDAN; Jeff Newmiller
Cc : [hidden email]
Objet : Re: [R] Genuine relative paths with R

On 10/10/2018 5:31 PM, Olivier GIVAUDAN wrote:
> I do not want to use the terminal, just double clicks (i.e. the
> simplest, automatic, non-manual way, without having to write a line /
> command).
> Therefore everything should happen outside any terminal. The user won't
> use a terminal.
>
> I don't have a Mac and I'm not familiar with this OS, sorry.
> But I'm really surprised the click method gives different results than

> I know the click method worked both on Linux (Ubuntu latest version) and

>
> Yes, I executed my file from a terminal and got obviously the same
> result as you (that's reassuring).
>
> Come on guys, creating a package... It's like using a hammer to kill a
> fly...

It's a simple operation to create a package in RStudio.  Not quite a
single click, but just a few.

In plain R, it's just a little more work using package.skeleton().

Really, if you are distributing R code, you should do it in the standard
way, not invent your own.

Duncan Murdoch

> ------------------------------------------------------------------------
> *De :* Duncan Murdoch <[hidden email]>
> *Envoy� :* mercredi 10 octobre 2018 20:54
> *� :* Olivier GIVAUDAN; Jeff Newmiller
> *Cc :* [hidden email]
> *Objet :* Re: [R] Genuine relative paths with R
> On 10/10/2018 4:42 PM, Olivier GIVAUDAN wrote:
>> Why are you not simply double-clicking on 'TestPWD' and choosing to
>> execute the file (don't add anything)?
>> Are you executing the file from a terminal?
>
> Yes, I was executing the file from my terminal.  Otherwise I really have
> no idea what the "current directory" is in the Finder.   (I'm on a Mac.
> I just tried the click method; it printed my home directory, not the
> directory of the script.)
>
> I don't know the name of your visual front end, but you are displaying
> the working directory that it sets when you click on TestPWD.  That will
> be different from the working directory that your user sees in the Terminal.
>
> You can see what I saw if you run TestPWD from the Terminal.  It will
> print the current working directory, not the one where TestPWD happens
> to live.
>
> If you want to do the same sort of thing in R, you could set up a script
> that calls R, and execute that in the way you executed TestPWD.  But in
> another message you said you aren't allowed to do that, so I think your
> best solution is the one offered by Bill Dunlap:  organize your files as
> an R package.  If you name your package "Olivier", then you can find all
> the files in it under the directory returned by
>
>     system.file(".", package = "Olivier")
>
> The package system is designed for R code, but you can put arbitrary
> files into a package:  just store them under the "inst" directory in
> your source.  When the package is installed, those files will be moved
> up one level, i.e.
>
> Olivier/inst/foo
>
> will become
>
>     system.file("foo", package = "Olivier")
>
> Duncan Murdoch

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Genuine relative paths with R

Duncan Murdoch-2
On 10/10/2018 5:45 PM, Olivier GIVAUDAN wrote:
> I'm not sure I'm "inventing my own way" of distributing R code... And I
> distribute it to a very limited audience.

Nothing says a package has to go on CRAN.  You can distribute them
privately to a small audience.

> Anyway, why not "inventing a new way" if it's more efficient than the
> standard one (I'm talking now in theory)?

If you know as much about R as the people who wrote it, then you can
almost certainly invent better ways to do many of the things it does.  R
Core was constrained by trying to maintain back compatibility, and that
means some of their solutions aren't perfect.

But if you don't know it that well, chances are you'll make mistakes
when you invent your own way of doing it.  For example, you might think
that all front ends set the working directory to the directory of the
program they are running, because the ones you've tried do it that way.
But they don't.

Duncan Murdoch

> ------------------------------------------------------------------------
> *De :* Duncan Murdoch <[hidden email]>
> *Envoyé :* mercredi 10 octobre 2018 21:39
> *À :* Olivier GIVAUDAN; Jeff Newmiller
> *Cc :* [hidden email]
> *Objet :* Re: [R] Genuine relative paths with R
> On 10/10/2018 5:31 PM, Olivier GIVAUDAN wrote:
>> I do not want to use the terminal, just double clicks (i.e. the
>> simplest, automatic, non-manual way, without having to write a line /
>> command).
>> Therefore everything should happen outside any terminal. The user won't
>> use a terminal.
>>
>> I don't have a Mac and I'm not familiar with this OS, sorry.
>> But I'm really surprised the click method gives different results than
>> on Linux and Windows.
>> I know the click method worked both on Linux (Ubuntu latest version) and
>> Windows (10).
>>
>> Yes, I executed my file from a terminal and got obviously the same
>> result as you (that's reassuring).
>>
>> Come on guys, creating a package... It's like using a hammer to kill a
>> fly...
>
> It's a simple operation to create a package in RStudio.  Not quite a
> single click, but just a few.
>
> In plain R, it's just a little more work using package.skeleton().
>
> Really, if you are distributing R code, you should do it in the standard
> way, not invent your own.
>
> Duncan Murdoch
>
>> ------------------------------------------------------------------------
>> *De :* Duncan Murdoch <[hidden email]>
>> *Envoyé :* mercredi 10 octobre 2018 20:54
>> *À :* Olivier GIVAUDAN; Jeff Newmiller
>> *Cc :* [hidden email]
>> *Objet :* Re: [R] Genuine relative paths with R
>> On 10/10/2018 4:42 PM, Olivier GIVAUDAN wrote:
>>> Why are you not simply double-clicking on 'TestPWD' and choosing to
>>> execute the file (don't add anything)?
>>> Are you executing the file from a terminal?
>>
>> Yes, I was executing the file from my terminal.  Otherwise I really have
>> no idea what the "current directory" is in the Finder.   (I'm on a Mac.
>> I just tried the click method; it printed my home directory, not the
>> directory of the script.)
>>
>> I don't know the name of your visual front end, but you are displaying
>> the working directory that it sets when you click on TestPWD.  That will
>> be different from the working directory that your user sees in the Terminal.
>>
>> You can see what I saw if you run TestPWD from the Terminal.  It will
>> print the current working directory, not the one where TestPWD happens
>> to live.
>>
>> If you want to do the same sort of thing in R, you could set up a script
>> that calls R, and execute that in the way you executed TestPWD.  But in
>> another message you said you aren't allowed to do that, so I think your
>> best solution is the one offered by Bill Dunlap:  organize your files as
>> an R package.  If you name your package "Olivier", then you can find all
>> the files in it under the directory returned by
>>
>>     system.file(".", package = "Olivier")
>>
>> The package system is designed for R code, but you can put arbitrary
>> files into a package:  just store them under the "inst" directory in
>> your source.  When the package is installed, those files will be moved
>> up one level, i.e.
>>
>> Olivier/inst/foo
>>
>> will become
>>
>>     system.file("foo", package = "Olivier")
>>
>> Duncan Murdoch
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Genuine relative paths with R

Olivier GIVAUDAN
> Nothing says a package has to go on CRAN.  You can distribute them privately to a small audience.

Yes, I agree in theory. But this solution still violates my own proportionality principle.

> If you know as much about R as the people who wrote it

I didn't claim that (that's was a quite general / theoretical statement, not necessarily and only applicable to R).

> For example, you might think that all front ends set the working directory to the directory of the program they are running, because the ones you've tried do it that way. But they don't.

It runs that way at least on Windows with RStudio and R GUI and I know the recipients of my R code work on Windows with at least one of these 2 GUIs. So the workaround I finally found satisfies my current needs.
________________________________
De : Duncan Murdoch <[hidden email]>
Envoy� : mercredi 10 octobre 2018 22:07
� : Olivier GIVAUDAN; Jeff Newmiller
Cc : [hidden email]
Objet : Re: [R] Genuine relative paths with R

On 10/10/2018 5:45 PM, Olivier GIVAUDAN wrote:
> I'm not sure I'm "inventing my own way" of distributing R code... And I
> distribute it to a very limited audience.

Nothing says a package has to go on CRAN.  You can distribute them
privately to a small audience.

> Anyway, why not "inventing a new way" if it's more efficient than the
> standard one (I'm talking now in theory)?

If you know as much about R as the people who wrote it, then you can
almost certainly invent better ways to do many of the things it does.  R
Core was constrained by trying to maintain back compatibility, and that
means some of their solutions aren't perfect.

But if you don't know it that well, chances are you'll make mistakes
when you invent your own way of doing it.  For example, you might think
that all front ends set the working directory to the directory of the
program they are running, because the ones you've tried do it that way.
But they don't.

Duncan Murdoch

> ------------------------------------------------------------------------
> *De :* Duncan Murdoch <[hidden email]>
> *Envoy� :* mercredi 10 octobre 2018 21:39
> *� :* Olivier GIVAUDAN; Jeff Newmiller
> *Cc :* [hidden email]
> *Objet :* Re: [R] Genuine relative paths with R
> On 10/10/2018 5:31 PM, Olivier GIVAUDAN wrote:
>> I do not want to use the terminal, just double clicks (i.e. the
>> simplest, automatic, non-manual way, without having to write a line /
>> command).
>> Therefore everything should happen outside any terminal. The user won't
>> use a terminal.
>>
>> I don't have a Mac and I'm not familiar with this OS, sorry.
>> But I'm really surprised the click method gives different results than

>> I know the click method worked both on Linux (Ubuntu latest version) and

>>
>> Yes, I executed my file from a terminal and got obviously the same
>> result as you (that's reassuring).
>>
>> Come on guys, creating a package... It's like using a hammer to kill a
>> fly...
>
> It's a simple operation to create a package in RStudio.  Not quite a
> single click, but just a few.
>
> In plain R, it's just a little more work using package.skeleton().
>
> Really, if you are distributing R code, you should do it in the standard
> way, not invent your own.
>
> Duncan Murdoch
>
>> ------------------------------------------------------------------------
>> *De :* Duncan Murdoch <[hidden email]>
>> *Envoy� :* mercredi 10 octobre 2018 20:54
>> *� :* Olivier GIVAUDAN; Jeff Newmiller
>> *Cc :* [hidden email]
>> *Objet :* Re: [R] Genuine relative paths with R
>> On 10/10/2018 4:42 PM, Olivier GIVAUDAN wrote:
>>> Why are you not simply double-clicking on 'TestPWD' and choosing to
>>> execute the file (don't add anything)?
>>> Are you executing the file from a terminal?
>>
>> Yes, I was executing the file from my terminal.  Otherwise I really have
>> no idea what the "current directory" is in the Finder.   (I'm on a Mac.
>> I just tried the click method; it printed my home directory, not the
>> directory of the script.)
>>
>> I don't know the name of your visual front end, but you are displaying
>> the working directory that it sets when you click on TestPWD.  That will
>> be different from the working directory that your user sees in the Terminal.
>>
>> You can see what I saw if you run TestPWD from the Terminal.  It will
>> print the current working directory, not the one where TestPWD happens
>> to live.
>>
>> If you want to do the same sort of thing in R, you could set up a script
>> that calls R, and execute that in the way you executed TestPWD.  But in
>> another message you said you aren't allowed to do that, so I think your
>> best solution is the one offered by Bill Dunlap:  organize your files as
>> an R package.  If you name your package "Olivier", then you can find all
>> the files in it under the directory returned by
>>
>>     system.file(".", package = "Olivier")
>>
>> The package system is designed for R code, but you can put arbitrary
>> files into a package:  just store them under the "inst" directory in
>> your source.  When the package is installed, those files will be moved
>> up one level, i.e.
>>
>> Olivier/inst/foo
>>
>> will become
>>
>>     system.file("foo", package = "Olivier")
>>
>> Duncan Murdoch
>

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Genuine relative paths with R

Duncan Murdoch-2
On 10/10/2018 6:17 PM, Olivier GIVAUDAN wrote:
>> Nothing says a package has to go on CRAN.  You can distribute
> themprivately to a small audience.
>
> Yes, I agree in theory. But this solution still violates my own
> proportionality principle.

Again, you seem to think making a package is a big deal.  Maybe that was
true 10 years ago (though I wrote and tested a package in a 45 minute
presentation at UseR 2008), but now it's very easy.

But you're free to decide not to do it:  just please don't repeat false
claims about R (like the ones about paths that started this long thread).

>
>> If you know as much about R as the people who wrote it
>
> I didn't claim that (that's was a quite general / theoretical statement,
> not necessarily and only applicable to R).

I didn't say you made that claim.  I was answering your question about
why inventing your own way is not a good idea.  It might be a good idea,
if you know the system very, very well.  Otherwise, it's probably better
to work the standard way.

Duncan Murdoch

>
>> For example, you might thinkthat all front ends set the working
> directory to the directory of theprogram they are running, because the
> ones you've tried do it that way. But they don't.
>
> It runs that way at least on Windows with RStudio and R GUI and I know
> the recipients of my R code work on Windows with at least one of these 2
> GUIs. So the workaround I finally found satisfies my current needs
> ------------------------------------------------------------------------
> *De :* Duncan Murdoch <[hidden email]>
> *Envoyé :* mercredi 10 octobre 2018 22:07
> *À :* Olivier GIVAUDAN; Jeff Newmiller
> *Cc :* [hidden email]
> *Objet :* Re: [R] Genuine relative paths with R
> On 10/10/2018 5:45 PM, Olivier GIVAUDAN wrote:
>> I'm not sure I'm "inventing my own way" of distributing R code... And I
>> distribute it to a very limited audience.
>
> Nothing says a package has to go on CRAN.  You can distribute them
> privately to a small audience.
>
>> Anyway, why not "inventing a new way" if it's more efficient than the
>> standard one (I'm talking now in theory)?
>
> If you know as much about R as the people who wrote it, then you can
> almost certainly invent better ways to do many of the things it does.  R
> Core was constrained by trying to maintain back compatibility, and that
> means some of their solutions aren't perfect.
>
> But if you don't know it that well, chances are you'll make mistakes
> when you invent your own way of doing it.  For example, you might think
> that all front ends set the working directory to the directory of the
> program they are running, because the ones you've tried do it that way.
> But they don't.
>
> Duncan Murdoch
>
>> ------------------------------------------------------------------------
>> *De :* Duncan Murdoch <[hidden email]>
>> *Envoyé :* mercredi 10 octobre 2018 21:39
>> *À :* Olivier GIVAUDAN; Jeff Newmiller
>> *Cc :* [hidden email]
>> *Objet :* Re: [R] Genuine relative paths with R
>> On 10/10/2018 5:31 PM, Olivier GIVAUDAN wrote:
>>> I do not want to use the terminal, just double clicks (i.e. the
>>> simplest, automatic, non-manual way, without having to write a line /
>>> command).
>>> Therefore everything should happen outside any terminal. The user won't
>>> use a terminal.
>>>
>>> I don't have a Mac and I'm not familiar with this OS, sorry.
>>> But I'm really surprised the click method gives different results than
>>> on Linux and Windows.
>>> I know the click method worked both on Linux (Ubuntu latest version) and
>>> Windows (10).
>>>
>>> Yes, I executed my file from a terminal and got obviously the same
>>> result as you (that's reassuring).
>>>
>>> Come on guys, creating a package... It's like using a hammer to kill a
>>> fly...
>>
>> It's a simple operation to create a package in RStudio.  Not quite a
>> single click, but just a few.
>>
>> In plain R, it's just a little more work using package.skeleton().
>>
>> Really, if you are distributing R code, you should do it in the standard
>> way, not invent your own.
>>
>> Duncan Murdoch
>>
>>> ------------------------------------------------------------------------
>>> *De :* Duncan Murdoch <[hidden email]>
>>> *Envoyé :* mercredi 10 octobre 2018 20:54
>>> *À :* Olivier GIVAUDAN; Jeff Newmiller
>>> *Cc :* [hidden email]
>>> *Objet :* Re: [R] Genuine relative paths with R
>>> On 10/10/2018 4:42 PM, Olivier GIVAUDAN wrote:
>>>> Why are you not simply double-clicking on 'TestPWD' and choosing to
>>>> execute the file (don't add anything)?
>>>> Are you executing the file from a terminal?
>>>
>>> Yes, I was executing the file from my terminal.  Otherwise I really have
>>> no idea what the "current directory" is in the Finder.   (I'm on a Mac.
>>> I just tried the click method; it printed my home directory, not the
>>> directory of the script.)
>>>
>>> I don't know the name of your visual front end, but you are displaying
>>> the working directory that it sets when you click on TestPWD.  That will
>>> be different from the working directory that your user sees in the Terminal.
>>>
>>> You can see what I saw if you run TestPWD from the Terminal.  It will
>>> print the current working directory, not the one where TestPWD happens
>>> to live.
>>>
>>> If you want to do the same sort of thing in R, you could set up a script
>>> that calls R, and execute that in the way you executed TestPWD.  But in
>>> another message you said you aren't allowed to do that, so I think your
>>> best solution is the one offered by Bill Dunlap:  organize your files as
>>> an R package.  If you name your package "Olivier", then you can find all
>>> the files in it under the directory returned by
>>>
>>>     system.file(".", package = "Olivier")
>>>
>>> The package system is designed for R code, but you can put arbitrary
>>> files into a package:  just store them under the "inst" directory in
>>> your source.  When the package is installed, those files will be moved
>>> up one level, i.e.
>>>
>>> Olivier/inst/foo
>>>
>>> will become
>>>
>>>     system.file("foo", package = "Olivier")
>>>
>>> Duncan Murdoch
>>
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Genuine relative paths with R

Olivier GIVAUDAN
> Again, you seem to think making a package is a big deal.

Perhaps not a big deal (I believe you, I didn't write an R package yet), but not as straightforward as having a function within an R file returning its own path.

> But you're free to decide not to do it:  just please don't repeat false claims about R (like the ones about paths that started this long thread).

Which false claims?
________________________________
De : Duncan Murdoch <[hidden email]>
Envoy� : mercredi 10 octobre 2018 22:31
� : Olivier GIVAUDAN; Jeff Newmiller
Cc : [hidden email]
Objet : Re: [R] Genuine relative paths with R

On 10/10/2018 6:17 PM, Olivier GIVAUDAN wrote:
>> Nothing says a package has to go on CRAN.  You can distribute
> themprivately to a small audience.
>
> Yes, I agree in theory. But this solution still violates my own
> proportionality principle.

Again, you seem to think making a package is a big deal.  Maybe that was
true 10 years ago (though I wrote and tested a package in a 45 minute
presentation at UseR 2008), but now it's very easy.

But you're free to decide not to do it:  just please don't repeat false
claims about R (like the ones about paths that started this long thread).

>
>> If you know as much about R as the people who wrote it
>
> I didn't claim that (that's was a quite general / theoretical statement,
> not necessarily and only applicable to R).

I didn't say you made that claim.  I was answering your question about
why inventing your own way is not a good idea.  It might be a good idea,
if you know the system very, very well.  Otherwise, it's probably better
to work the standard way.

Duncan Murdoch

>
>> For example, you might thinkthat all front ends set the working
> directory to the directory of theprogram they are running, because the
> ones you've tried do it that way. But they don't.
>


> GUIs. So the workaround I finally found satisfies my current needs
> ------------------------------------------------------------------------
> *De :* Duncan Murdoch <[hidden email]>
> *Envoy� :* mercredi 10 octobre 2018 22:07
> *� :* Olivier GIVAUDAN; Jeff Newmiller
> *Cc :* [hidden email]
> *Objet :* Re: [R] Genuine relative paths with R
> On 10/10/2018 5:45 PM, Olivier GIVAUDAN wrote:
>> I'm not sure I'm "inventing my own way" of distributing R code... And I
>> distribute it to a very limited audience.
>
> Nothing says a package has to go on CRAN.  You can distribute them
> privately to a small audience.
>
>> Anyway, why not "inventing a new way" if it's more efficient than the
>> standard one (I'm talking now in theory)?
>
> If you know as much about R as the people who wrote it, then you can
> almost certainly invent better ways to do many of the things it does.  R
> Core was constrained by trying to maintain back compatibility, and that
> means some of their solutions aren't perfect.
>
> But if you don't know it that well, chances are you'll make mistakes
> when you invent your own way of doing it.  For example, you might think
> that all front ends set the working directory to the directory of the
> program they are running, because the ones you've tried do it that way.
> But they don't.
>
> Duncan Murdoch
>
>> ------------------------------------------------------------------------
>> *De :* Duncan Murdoch <[hidden email]>
>> *Envoy� :* mercredi 10 octobre 2018 21:39
>> *� :* Olivier GIVAUDAN; Jeff Newmiller
>> *Cc :* [hidden email]
>> *Objet :* Re: [R] Genuine relative paths with R
>> On 10/10/2018 5:31 PM, Olivier GIVAUDAN wrote:
>>> I do not want to use the terminal, just double clicks (i.e. the
>>> simplest, automatic, non-manual way, without having to write a line /
>>> command).
>>> Therefore everything should happen outside any terminal. The user won't
>>> use a terminal.
>>>
>>> I don't have a Mac and I'm not familiar with this OS, sorry.
>>> But I'm really surprised the click method gives different results than

>>> I know the click method worked both on Linux (Ubuntu latest version) and

>>>
>>> Yes, I executed my file from a terminal and got obviously the same
>>> result as you (that's reassuring).
>>>
>>> Come on guys, creating a package... It's like using a hammer to kill a
>>> fly...
>>
>> It's a simple operation to create a package in RStudio.  Not quite a
>> single click, but just a few.
>>
>> In plain R, it's just a little more work using package.skeleton().
>>
>> Really, if you are distributing R code, you should do it in the standard
>> way, not invent your own.
>>
>> Duncan Murdoch
>>
>>> ------------------------------------------------------------------------
>>> *De :* Duncan Murdoch <[hidden email]>
>>> *Envoy� :* mercredi 10 octobre 2018 20:54
>>> *� :* Olivier GIVAUDAN; Jeff Newmiller
>>> *Cc :* [hidden email]
>>> *Objet :* Re: [R] Genuine relative paths with R
>>> On 10/10/2018 4:42 PM, Olivier GIVAUDAN wrote:
>>>> Why are you not simply double-clicking on 'TestPWD' and choosing to
>>>> execute the file (don't add anything)?
>>>> Are you executing the file from a terminal?
>>>
>>> Yes, I was executing the file from my terminal.  Otherwise I really have
>>> no idea what the "current directory" is in the Finder.   (I'm on a Mac.
>>> I just tried the click method; it printed my home directory, not the
>>> directory of the script.)
>>>
>>> I don't know the name of your visual front end, but you are displaying
>>> the working directory that it sets when you click on TestPWD.  That will
>>> be different from the working directory that your user sees in the Terminal.
>>>
>>> You can see what I saw if you run TestPWD from the Terminal.  It will
>>> print the current working directory, not the one where TestPWD happens
>>> to live.
>>>
>>> If you want to do the same sort of thing in R, you could set up a script
>>> that calls R, and execute that in the way you executed TestPWD.  But in
>>> another message you said you aren't allowed to do that, so I think your
>>> best solution is the one offered by Bill Dunlap:  organize your files as
>>> an R package.  If you name your package "Olivier", then you can find all
>>> the files in it under the directory returned by
>>>
>>>     system.file(".", package = "Olivier")
>>>
>>> The package system is designed for R code, but you can put arbitrary
>>> files into a package:  just store them under the "inst" directory in
>>> your source.  When the package is installed, those files will be moved
>>> up one level, i.e.
>>>
>>> Olivier/inst/foo
>>>
>>> will become
>>>
>>>     system.file("foo", package = "Olivier")
>>>
>>> Duncan Murdoch
>>
>

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Genuine relative paths with R

Duncan Murdoch-2
On 10/10/2018 6:52 PM, Olivier GIVAUDAN wrote:

>>  Again, you seem to think making a package is a big deal.
>
> Perhaps not a big deal (I believe you, I didn't write an R package yet),
> but not as straightforward as having a function within an R file
> returning its own path.
>
>> But you're free to decide not to do it:  just please don't repeat
> falseclaims about R (like the ones about paths that started this long
> thread).
>
> Which false claims?

"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.)?"

Duncan Murdoch

> ------------------------------------------------------------------------
> *De :* Duncan Murdoch <[hidden email]>
> *Envoyé :* mercredi 10 octobre 2018 22:31
> *À :* Olivier GIVAUDAN; Jeff Newmiller
> *Cc :* [hidden email]
> *Objet :* Re: [R] Genuine relative paths with R
> On 10/10/2018 6:17 PM, Olivier GIVAUDAN wrote:
>>> Nothing says a package has to go on CRAN.  You can distribute
>> themprivately to a small audience.
>>
>> Yes, I agree in theory. But this solution still violates my own
>> proportionality principle.
>
> Again, you seem to think making a package is a big deal.  Maybe that was
> true 10 years ago (though I wrote and tested a package in a 45 minute
> presentation at UseR 2008), but now it's very easy.
>
> But you're free to decide not to do it:  just please don't repeat false
> claims about R (like the ones about paths that started this long thread).
>
>>
>>> If you know as much about R as the people who wrote it
>>
>> I didn't claim that (that's was a quite general / theoretical statement,
>> not necessarily and only applicable to R).
>
> I didn't say you made that claim.  I was answering your question about
> why inventing your own way is not a good idea.  It might be a good idea,
> if you know the system very, very well.  Otherwise, it's probably better
> to work the standard way.
>
> Duncan Murdoch
>
>>
>>> For example, you might thinkthat all front ends set the working
>> directory to the directory of theprogram they are running, because the
>> ones you've tried do it that way. But they don't.
>>
>> It runs that way at least on Windows with RStudio and R GUI and I know
>> the recipients of my R code work on Windows with at least one of these 2
>> GUIs. So the workaround I finally found satisfies my current needs
>> ------------------------------------------------------------------------
>> *De :* Duncan Murdoch <[hidden email]>
>> *Envoyé :* mercredi 10 octobre 2018 22:07
>> *À :* Olivier GIVAUDAN; Jeff Newmiller
>> *Cc :* [hidden email]
>> *Objet :* Re: [R] Genuine relative paths with R
>> On 10/10/2018 5:45 PM, Olivier GIVAUDAN wrote:
>>> I'm not sure I'm "inventing my own way" of distributing R code... And I
>>> distribute it to a very limited audience.
>>
>> Nothing says a package has to go on CRAN.  You can distribute them
>> privately to a small audience.
>>
>>> Anyway, why not "inventing a new way" if it's more efficient than the
>>> standard one (I'm talking now in theory)?
>>
>> If you know as much about R as the people who wrote it, then you can
>> almost certainly invent better ways to do many of the things it does.  R
>> Core was constrained by trying to maintain back compatibility, and that
>> means some of their solutions aren't perfect.
>>
>> But if you don't know it that well, chances are you'll make mistakes
>> when you invent your own way of doing it.  For example, you might think
>> that all front ends set the working directory to the directory of the
>> program they are running, because the ones you've tried do it that way.
>> But they don't.
>>
>> Duncan Murdoch
>>
>>> ------------------------------------------------------------------------
>>> *De :* Duncan Murdoch <[hidden email]>
>>> *Envoyé :* mercredi 10 octobre 2018 21:39
>>> *À :* Olivier GIVAUDAN; Jeff Newmiller
>>> *Cc :* [hidden email]
>>> *Objet :* Re: [R] Genuine relative paths with R
>>> On 10/10/2018 5:31 PM, Olivier GIVAUDAN wrote:
>>>> I do not want to use the terminal, just double clicks (i.e. the
>>>> simplest, automatic, non-manual way, without having to write a line /
>>>> command).
>>>> Therefore everything should happen outside any terminal. The user won't
>>>> use a terminal.
>>>>
>>>> I don't have a Mac and I'm not familiar with this OS, sorry.
>>>> But I'm really surprised the click method gives different results than
>>>> on Linux and Windows.
>>>> I know the click method worked both on Linux (Ubuntu latest version) and
>>>> Windows (10).
>>>>
>>>> Yes, I executed my file from a terminal and got obviously the same
>>>> result as you (that's reassuring).
>>>>
>>>> Come on guys, creating a package... It's like using a hammer to kill a
>>>> fly...
>>>
>>> It's a simple operation to create a package in RStudio.  Not quite a
>>> single click, but just a few.
>>>
>>> In plain R, it's just a little more work using package.skeleton().
>>>
>>> Really, if you are distributing R code, you should do it in the standard
>>> way, not invent your own.
>>>
>>> Duncan Murdoch
>>>
>>>> ------------------------------------------------------------------------
>>>> *De :* Duncan Murdoch <[hidden email]>
>>>> *Envoyé :* mercredi 10 octobre 2018 20:54
>>>> *À :* Olivier GIVAUDAN; Jeff Newmiller
>>>> *Cc :* [hidden email]
>>>> *Objet :* Re: [R] Genuine relative paths with R
>>>> On 10/10/2018 4:42 PM, Olivier GIVAUDAN wrote:
>>>>> Why are you not simply double-clicking on 'TestPWD' and choosing to
>>>>> execute the file (don't add anything)?
>>>>> Are you executing the file from a terminal?
>>>>
>>>> Yes, I was executing the file from my terminal.  Otherwise I really have
>>>> no idea what the "current directory" is in the Finder.   (I'm on a Mac.
>>>> I just tried the click method; it printed my home directory, not the
>>>> directory of the script.)
>>>>
>>>> I don't know the name of your visual front end, but you are displaying
>>>> the working directory that it sets when you click on TestPWD.  That will
>>>> be different from the working directory that your user sees in the Terminal.
>>>>
>>>> You can see what I saw if you run TestPWD from the Terminal.  It will
>>>> print the current working directory, not the one where TestPWD happens
>>>> to live.
>>>>
>>>> If you want to do the same sort of thing in R, you could set up a script
>>>> that calls R, and execute that in the way you executed TestPWD.  But in
>>>> another message you said you aren't allowed to do that, so I think your
>>>> best solution is the one offered by Bill Dunlap:  organize your files as
>>>> an R package.  If you name your package "Olivier", then you can find all
>>>> the files in it under the directory returned by
>>>>
>>>>     system.file(".", package = "Olivier")
>>>>
>>>> The package system is designed for R code, but you can put arbitrary
>>>> files into a package:  just store them under the "inst" directory in
>>>> your source.  When the package is installed, those files will be moved
>>>> up one level, i.e.
>>>>
>>>> Olivier/inst/foo
>>>>
>>>> will become
>>>>
>>>>     system.file("foo", package = "Olivier")
>>>>
>>>> Duncan Murdoch
>>>
>>
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Genuine relative paths with R

Olivier GIVAUDAN
It is not wrong to claim that R currently doesn't have a function returning the path of the R file where this same function was invoked.

'getwd()' is indeed not equivalent to VBA 'Application.ThisWorkbook.Path' or C macro '__FILE__' or SAS %sysget(SAS_EXECFILENAME), etc.
________________________________
De : Duncan Murdoch <[hidden email]>
Envoy� : mercredi 10 octobre 2018 22:59
� : Olivier GIVAUDAN; Jeff Newmiller
Cc : [hidden email]
Objet : Re: [R] Genuine relative paths with R

On 10/10/2018 6:52 PM, Olivier GIVAUDAN wrote:

>>  Again, you seem to think making a package is a big deal.
>
> Perhaps not a big deal (I believe you, I didn't write an R package yet),
> but not as straightforward as having a function within an R file
> returning its own path.
>
>> But you're free to decide not to do it:  just please don't repeat
> falseclaims about R (like the ones about paths that started this long
> thread).
>
> Which false claims?
"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.)?"

Duncan Murdoch

> ------------------------------------------------------------------------
> *De :* Duncan Murdoch <[hidden email]>
> *Envoy� :* mercredi 10 octobre 2018 22:31
> *� :* Olivier GIVAUDAN; Jeff Newmiller
> *Cc :* [hidden email]
> *Objet :* Re: [R] Genuine relative paths with R
> On 10/10/2018 6:17 PM, Olivier GIVAUDAN wrote:
>>> Nothing says a package has to go on CRAN.  You can distribute
>> themprivately to a small audience.
>>
>> Yes, I agree in theory. But this solution still violates my own
>> proportionality principle.
>
> Again, you seem to think making a package is a big deal.  Maybe that was
> true 10 years ago (though I wrote and tested a package in a 45 minute
> presentation at UseR 2008), but now it's very easy.
>
> But you're free to decide not to do it:  just please don't repeat false
> claims about R (like the ones about paths that started this long thread).
>
>>
>>> If you know as much about R as the people who wrote it
>>
>> I didn't claim that (that's was a quite general / theoretical statement,
>> not necessarily and only applicable to R).
>
> I didn't say you made that claim.  I was answering your question about
> why inventing your own way is not a good idea.  It might be a good idea,
> if you know the system very, very well.  Otherwise, it's probably better
> to work the standard way.
>
> Duncan Murdoch
>
>>
>>> For example, you might thinkthat all front ends set the working
>> directory to the directory of theprogram they are running, because the
>> ones you've tried do it that way. But they don't.
>>

>> GUIs. So the workaround I finally found satisfies my current needs
>> ------------------------------------------------------------------------
>> *De :* Duncan Murdoch <[hidden email]>
>> *Envoy� :* mercredi 10 octobre 2018 22:07
>> *� :* Olivier GIVAUDAN; Jeff Newmiller
>> *Cc :* [hidden email]
>> *Objet :* Re: [R] Genuine relative paths with R
>> On 10/10/2018 5:45 PM, Olivier GIVAUDAN wrote:
>>> I'm not sure I'm "inventing my own way" of distributing R code... And I
>>> distribute it to a very limited audience.
>>
>> Nothing says a package has to go on CRAN.  You can distribute them
>> privately to a small audience.
>>
>>> Anyway, why not "inventing a new way" if it's more efficient than the
>>> standard one (I'm talking now in theory)?
>>
>> If you know as much about R as the people who wrote it, then you can
>> almost certainly invent better ways to do many of the things it does.  R
>> Core was constrained by trying to maintain back compatibility, and that
>> means some of their solutions aren't perfect.
>>
>> But if you don't know it that well, chances are you'll make mistakes
>> when you invent your own way of doing it.  For example, you might think
>> that all front ends set the working directory to the directory of the
>> program they are running, because the ones you've tried do it that way.
>> But they don't.
>>
>> Duncan Murdoch
>>
>>> ------------------------------------------------------------------------
>>> *De :* Duncan Murdoch <[hidden email]>
>>> *Envoy� :* mercredi 10 octobre 2018 21:39
>>> *� :* Olivier GIVAUDAN; Jeff Newmiller
>>> *Cc :* [hidden email]
>>> *Objet :* Re: [R] Genuine relative paths with R
>>> On 10/10/2018 5:31 PM, Olivier GIVAUDAN wrote:
>>>> I do not want to use the terminal, just double clicks (i.e. the
>>>> simplest, automatic, non-manual way, without having to write a line /
>>>> command).
>>>> Therefore everything should happen outside any terminal. The user won't
>>>> use a terminal.
>>>>
>>>> I don't have a Mac and I'm not familiar with this OS, sorry.
>>>> But I'm really surprised the click method gives different results than

>>>> I know the click method worked both on Linux (Ubuntu latest version) and

>>>>
>>>> Yes, I executed my file from a terminal and got obviously the same
>>>> result as you (that's reassuring).
>>>>
>>>> Come on guys, creating a package... It's like using a hammer to kill a
>>>> fly...
>>>
>>> It's a simple operation to create a package in RStudio.  Not quite a
>>> single click, but just a few.
>>>
>>> In plain R, it's just a little more work using package.skeleton().
>>>
>>> Really, if you are distributing R code, you should do it in the standard
>>> way, not invent your own.
>>>
>>> Duncan Murdoch
>>>
>>>> ------------------------------------------------------------------------
>>>> *De :* Duncan Murdoch <[hidden email]>
>>>> *Envoy� :* mercredi 10 octobre 2018 20:54
>>>> *� :* Olivier GIVAUDAN; Jeff Newmiller
>>>> *Cc :* [hidden email]
>>>> *Objet :* Re: [R] Genuine relative paths with R
>>>> On 10/10/2018 4:42 PM, Olivier GIVAUDAN wrote:
>>>>> Why are you not simply double-clicking on 'TestPWD' and choosing to
>>>>> execute the file (don't add anything)?
>>>>> Are you executing the file from a terminal?
>>>>
>>>> Yes, I was executing the file from my terminal.  Otherwise I really have
>>>> no idea what the "current directory" is in the Finder.   (I'm on a Mac.
>>>> I just tried the click method; it printed my home directory, not the
>>>> directory of the script.)
>>>>
>>>> I don't know the name of your visual front end, but you are displaying
>>>> the working directory that it sets when you click on TestPWD.  That will
>>>> be different from the working directory that your user sees in the Terminal.
>>>>
>>>> You can see what I saw if you run TestPWD from the Terminal.  It will
>>>> print the current working directory, not the one where TestPWD happens
>>>> to live.
>>>>
>>>> If you want to do the same sort of thing in R, you could set up a script
>>>> that calls R, and execute that in the way you executed TestPWD.  But in
>>>> another message you said you aren't allowed to do that, so I think your
>>>> best solution is the one offered by Bill Dunlap:  organize your files as
>>>> an R package.  If you name your package "Olivier", then you can find all
>>>> the files in it under the directory returned by
>>>>
>>>>     system.file(".", package = "Olivier")
>>>>
>>>> The package system is designed for R code, but you can put arbitrary
>>>> files into a package:  just store them under the "inst" directory in
>>>> your source.  When the package is installed, those files will be moved
>>>> up one level, i.e.
>>>>
>>>> Olivier/inst/foo
>>>>
>>>> will become
>>>>
>>>>     system.file("foo", package = "Olivier")
>>>>
>>>> Duncan Murdoch
>>>
>>
>

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Genuine relative paths with R

R help mailing list-2
If I’m following all this correctly, it seems your criticism is that R doesn’t provide a run-time function that is equivalent to a compile-time macro. You do realize that __FILE__ is not part of the C programming language - it’s a predefined variable recognized by the cpp - the C preprocessor program. Similarly, SAS_EXECFILENAME is a predefined variable that can be recognized by the SAS macro processor, but has no meaning in the SAS language itself.

So, to go back to your original post:
> 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.


This is what a macro processor does. Are you asking for a macro language in R?

Cheers

> On Oct 10, 2018, at 6:11 PM, Olivier GIVAUDAN <[hidden email]> wrote:
>
> It is not wrong to claim that R currently doesn't have a function returning the path of the R file where this same function was invoked.
>
> 'getwd()' is indeed not equivalent to VBA 'Application.ThisWorkbook.Path' or C macro '__FILE__' or SAS %sysget(SAS_EXECFILENAME), etc.


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
123