Skip to content
Prev 43387 / 63421 Next

Best way to locate R executable from within R?

On Tue, May 22, 2012 at 1:34 PM, Henrik Bengtsson <hb at biostat.ucsf.edu> wrote:
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)