[R] Genuine relative paths with R

Olivier GIVAUDAN
Sat, 06 Oct 2018 21:35:36 -0000

Hi Ista,

No worry :)

"/path/to/dir" is what I call hardcoded path.
And I do want to work with relative paths in code, not interacticely.

I (but not only I actually!) need genuine relative paths especially for
inputting or outputting files in a location "close" (e.g. parent or
child folder) to where the R script is located.

I'm looking for the equivalent of the function cd in batch / shell, for
instance, or __DIR__ in PHP, or %sysget(SAS_EXECFILEPATH) in SAS, etc.

Is it clearer now?

Best regards,

Olivier

________________________________
De : Ista Zahn <istazahn at gmail.com>
Envoy? : samedi 6 octobre 2018 16:40
? : olivier_givaudan at hotmail.com; r-help at r-project.org
Objet : Re: [R] Genuine relative paths with R

Hi Olivier,

Sorry for misspelling your name! Please see inline below.

On Sat, Oct 6, 2018 at 9:58 AM Olivier GIVAUDAN
<olivier_givaudan at hotmail.com> wrote:
>
> Hi Ista,
>
> Thank you for your reply.
>
> My motivation is described at the 3rd line of my initial message:
> "if I move all my scripts related to some project as a whole to
another location of my computer or someone else's computer, if want my
scripts to continue to run seamlessly."
>
> The main point is simply to avoid any hardcoded path in my R scripts, as
you wrote in your examples.

My claim is that you cannot avoid hardcoding paths. Even if the
feature you requested existed, you would still have to do e.g.,

source("/path/to/dir/file.R")

That is just as much hard-coded as the (only slightly more verbose)

cd("/path/to/dir")
source("file.R")

So, I'm afraid I still don't get it. What is it that you actually want
to do that you currently cannot?

Best,
Ista
>
> BTW I don't use R with command line, only within RStudio or with the
default GUI.
>
> Best regards,
>
> Olivier (and not Oliver )
> ________________________________
> De : Ista Zahn <istazahn at gmail.com>
> Envoy? : samedi 6 octobre 2018 12:56
> ? : olivier_givaudan at hotmail.com
> Cc : r-help at r-project.org
> Objet : Re: [R] Genuine relative paths with R
>
> Hi Oliver,
>
> Interesting question. Can you describe your motivation in a little
> more detail? That is, why do you what this feature? I ask because to
> my way of thinking you have to know the path to the script in order to
> call it in the first place. If calling from R, is
>
> setwd("/path/to/dir")
> source("file.R")
>
> really so much worse than
>
> source("path/to/dir/file.R")
>
> ?  If it is worse, why?
>
> Similarly if calling from a shell, is
>
> cd /path/to/dir
> Rscript file.R
>
> really so much worse than
>
> Rscript /path/to/dir/file.R
>
> ? Again, if is worse, why?
>
> Best,
> Ista
> On Sat, Oct 6, 2018 at 8:25 AM Olivier GIVAUDAN
> <olivier_givaudan at hotmail.com> wrote:
> >
> > Dear R users,
> >
> > I would like to work with genuine relative paths in R for obvious
reasons: if I move all my scripts related to some project as a whole to another
location of my computer or someone else's computer, if want my scripts to
continue to run seamlessly.
> >
> > What I mean by "genuine" is that it should not be necessary
to hardcode one single absolute path (making the code obviously not
"portable" - to another place - anymore).
> >
> > For the time being, I found the following related posts, unfortunately
never conclusive or even somewhat off-topic:
> >
https://stackoverflow.com/questions/1815606/rscript-determine-path-of-the-executing-script
> >
https://stackoverflow.com/questions/47044068/get-the-path-of-current-script/47045368
> >
http://r.789695.n4.nabble.com/Script-auto-detecting-its-own-path-td2719676.html
> >
> > So I found 2 workarounds, more or less satisfactory:
> >
> >
> >   1.  Either create a variable "ScriptPath" in the first
lines of each of my R scripts and run a batch (or shell, etc.) to replace every
single occurrence of "ScriptPath <-" by "ScriptPath <-
[Absolute path of the R script]" in all the R scripts located in the folder
(and possibly subfolders) of the batch file.
> >   2.  Or create an R project file with RStudio and use the package
"here" to get the absolute path of the R project file and put all the
R scripts related to this project in the R project directory, as often
recommended.
> >
> > But I am really wondering why R doesn't have (please tell me if
I'm wrong) this basic feature as many other languages have it (batch, shell,
C, LaTeX, SAS with macro-variables, etc.)?
> > Do you know whether the language will have this kind of function in a
near future? What are the obstacles / what is the reasoning for not having it
already?
> >
> > Do you know other workarounds?
> >
> > Best regards,
> >
> > Olivier
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.

	[[alternative HTML version deleted]]