Skip to content
Prev 376831 / 398502 Next

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
---------------------------------------------------------------------------
Message-ID: <alpine.BSF.2.00.1810071155020.10784@pedal.dcn.davis.ca.us>
In-Reply-To: <VI1PR0202MB335728CFA9C4B2B31EAA4043F5E50@VI1PR0202MB3357.eurprd02.prod.outlook.com>

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