Skip to content
Prev 376875 / 398502 Next

Genuine relative paths with R

Hi Jeff,
?
?
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.?
?
?
That's precisely the reproach I have in case the code comes from a file.?
?
?
'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).?
?
?
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'.?
?
?
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.
?
?
Yes, using an .Rproj file to be opened with RStudio also seems to me to be a reasonable solution (the one I chose until now), although it is still a workaround and requires RStudio.?
Actually in this case, the package 'here'? I previously mentioned is useless to get the current working directory of the project as 'When a project is opened within RStudio the following actions are taken: [...] The current working directory is set to the project directory.', as stated on this page: https://support.rstudio.com/hc/en-us/articles/200526207-Using-Projects.
So 'getwd()' returns exactly the same as 'here()' does.

Best regards,?
?
Olivier

________________________________
De : Jeff Newmiller <jdnewmil at dcn.davis.ca.us>
Envoy? : dimanche 7 octobre 2018 20:48
? : Olivier GIVAUDAN
Cc : D?nes T?th; r-help at r-project.org
Objet : RE: [R] Genuine relative paths with R
On Sun, 7 Oct 2018, Olivier GIVAUDAN wrote:

            
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.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                       Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
---------------------------------------------------------------------------

Thread (35 messages)

Jeff Newmiller Genuine relative paths with R Oct 7 Olivier GIVAUDAN Genuine relative paths with R Oct 10 Duncan Murdoch Genuine relative paths with R Oct 10 Olivier GIVAUDAN Genuine relative paths with R Oct 10 Duncan Murdoch Genuine relative paths with R Oct 10 Duncan Murdoch Genuine relative paths with R Oct 10 William Dunlap Genuine relative paths with R Oct 10 Olivier GIVAUDAN Genuine relative paths with R Oct 10 Olivier GIVAUDAN Genuine relative paths with R Oct 10 Duncan Murdoch Genuine relative paths with R Oct 10 Olivier GIVAUDAN Genuine relative paths with R Oct 10 Duncan Murdoch Genuine relative paths with R Oct 10 Olivier GIVAUDAN Genuine relative paths with R Oct 10 Olivier GIVAUDAN Genuine relative paths with R Oct 10 Duncan Murdoch Genuine relative paths with R Oct 10 Olivier GIVAUDAN Genuine relative paths with R Oct 10 Duncan Murdoch Genuine relative paths with R Oct 10 Olivier GIVAUDAN Genuine relative paths with R Oct 10 Duncan Murdoch Genuine relative paths with R Oct 10 Olivier GIVAUDAN Genuine relative paths with R Oct 10 Duncan Murdoch Genuine relative paths with R Oct 10 Olivier GIVAUDAN Genuine relative paths with R Oct 10 Peter Claussen Genuine relative paths with R Oct 10 Duncan Murdoch Genuine relative paths with R Oct 10 Olivier GIVAUDAN Genuine relative paths with R Oct 10 Olivier GIVAUDAN Genuine relative paths with R Oct 10 Fritsch, Katharina (NNL) Genuine relative paths with R Oct 11 Duncan Murdoch Genuine relative paths with R Oct 11 Michael Friendly Genuine relative paths with R Oct 11 Olivier GIVAUDAN Genuine relative paths with R Oct 18 Olivier GIVAUDAN Genuine relative paths with R Oct 18 Olivier GIVAUDAN Genuine relative paths with R Oct 18 Olivier GIVAUDAN Genuine relative paths with R Oct 18 Fritsch, Katharina (NNL) Genuine relative paths with R Oct 19 Olivier GIVAUDAN Genuine relative paths with R Oct 19