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

Simon Urbanek simon.urbanek at r-project.org
Tue May 22 20:47:53 CEST 2012


On May 22, 2012, at 2:39 PM, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:

> On Tue, May 22, 2012 at 1:34 PM, 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?
>> 
> 
> At least on Windows one could run R via R.exe, Rterm.exe or Rgui.exe
> amd #2 would not pick up the differences.  On the other hand if I do
> this on the Windows command line on my Vista system with R 2.15.0
> patched:
> 
> cd \program files\R\R-2.15.x\bin\i386
> Rterm.exe
> 
> and then enter commandArgs() into R, the output is "Rterm.exe" with no path.
> 
> The fact that one can have 32 bit and 64 bit R executables on the same
> system complicates things too.
> 
> Thus, on Windows something like this might work:
> 
>   file.path(R.home("bin"), R.version$arch, basename(commandArgs()[[1]]))
> 
> If there are cases that I missed then this might pick up those too:
> 
>   R <- commandArgs()[[1]]
>   if (R == basename(R)) R <- file.path(R.home("bin"), R.version$arch, R)
> 

Typically, when you want another subprocess of R it is not the same as the process you're in - e.g. you certainly don't want to use Rgui to run a script, so the above is not useful for Henrik's purpose.
Besides, it won't even work since arg1 can as well be a symlink in a completely different place.

Cheers,
Simon


> -- 
> Statistics & Software Consulting
> GKX Group, GKX Associates Inc.
> tel: 1-877-GKX-GROUP
> email: ggrothendieck at gmail.com
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 
> 



More information about the R-devel mailing list