[Rd] Best way to locate R executable from within R?

Henrik Bengtsson hb at biostat.ucsf.edu
Tue May 22 20:03:28 CEST 2012


On Tue, May 22, 2012 at 10:47 AM, Dan Tenenbaum <dtenenba at fhcrc.org> wrote:
> On Tue, May 22, 2012 at 10:34 AM, Henrik Bengtsson <hb at biostat.ucsf.edu> wrote:
>> Hi,
>>
>> I'd like to spawn of a new R process from within R using system(),
>> e.g. system("R -f myScript.R").  However, just specifying "R" as in
>> that example is not guaranteed to work, because "R" may not be on the
>> OS's search path.
>>
>>  What is the best way, from within a running R, to infer the command
>> (basename or full path) for launching R in way that it works on any
>> OS?  I came up with the following alternatives, but I'm not sure if
>> they'll work everywhere or not:
>>
>> 1. Rbin <- commandArgs()[1];
>>
>> 2. Rbin <- file.path(R.home(), "bin", "R");
>>
>> Other suggestions that are better?
>
> Rbin <- file.path(Sys.getenv("R_HOME"), "bin", "R")

I don't see how Sys.getenv("R_HOME") would be better than R.home() -
any reasons?

Your reply triggered me to read up on help("R.home"), where I noticed
the passage:

"A character string giving the R home directory or path to a
particular component. Normally the components are all subdirectories
of the R home directory, but this may not be the case in a Unix-like
installation.  [...] The return value for "modules" and on Windows
"bin" is to a sub-architecture-specific location."

So, now I'd say that:

4. Rbin <- file.path(R.home("bin"), "R");

is better than (2).  I'm still not sure whether to use (1) or (4), though.

/Henrik

>
> Dan
>
>
>>
>> /Henrik
>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list