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

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Tue, 22 May 2012 20:50:25 -0400

On Tue, May 22, 2012 at 7:50 PM, Simon Urbanek <simon.urbanek_at_r-project.org> wrote:
>
> On May 22, 2012, at 7:37 PM, Gabor Grothendieck wrote:
>
>> On Tue, May 22, 2012 at 6:04 PM, Simon Urbanek
>> <simon.urbanek_at_r-project.org> wrote:
>>>
>>> On May 22, 2012, at 3:34 PM, Gabor Grothendieck wrote:
>>>
>>>> On Tue, May 22, 2012 at 3:28 PM, Gabor Grothendieck
>>>> <ggrothendieck_at_gmail.com> wrote:
>>>>> On Tue, May 22, 2012 at 3:05 PM, Henrik Bengtsson <hb_at_biostat.ucsf.edu> wrote:
>>>>>> On Tue, May 22, 2012 at 11:39 AM, 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.
>>>>>>
>>>>>> Thanks, I overlooked this need.  For my particular use case, I'm
>>>>>> interested in launching R in "batch" mode, so "R" will do (but not
>>>>>> "Rgui").
>>>>>>
>>>>>>>
>>>>>>> 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)
>>>>>>
>>>>>> FYI, R.home("bin") is not the same as file.path(R.home(), "bin"), cf.
>>>>>> help("R.home").  R.home("bin") will pick up the current architecture
>>>>>> directory (by using .Platform$r_arch), e.g.
>>>>>>
>>>>>>> R.home("bin")
>>>>>> [1] "C:/PROGRA~1/R/R-2.15.0patched/bin/x64"
>>>>>>
>>>>>> /Henrik
>>>>>>
>>>>>
>>>>> Then perhaps something like this which is still not 100% foolproof but
>>>>> should work most of the time:
>>>>>
>>>>> Find(file.exists, c(
>>>>>   commandArgs()[[1]],
>>>>>   file.path(R.home("bin"), commandArgs()[[1]]),
>>>>>   file.path(R.home("bin"), "R")
>>>>> ))
>>>>
>>>> So that the last one tried works on Windows too it should be:
>>>>
>>>> Find(file.exists, c(
>>>>   commandArgs()[[1]],
>>>>   file.path(R.home("bin"), commandArgs()[[1]]),
>>>>   file.path(R.home("bin"), "R"),
>>>>   file.path(R.home("bin"), "R.exe")
>>>> ))
>>>>
>>>
>>> Obviously, you don't want to do that for reasons discussed previously.
>>>
>>
>> In most cases with a link the complete path would be passed in which
>> case the first arg of Find would be chosen and be correct. If not it
>> would fail through to further choices one of which would likely be
>> correct and if none of them are then its likely that
>> file.path(R.home("bin"), "R")  isn't either since that is already one
>> of the choices.  While its not 100% foolproof the cases where it does
>> not work are quite pathological whereas the cases where
>> file.path(R.home("bin"), "R") fails to use the same executable include
>> common cases such as R being called as Rterm or Rscript.
>>
>
> Except that you may not have noticed that no one asked about that since that makes no sense (arguments differ etc.). The question was how to start R and your suggestions make it only worse and unusable - fortunately Henrik asked about better solutions so we can safely close this discussion.
>
> Cheers,
> Simon
>

So far he has excluded Rgui but that still leaves Rterm, Rscript and R (and littler on UNIX) -- all of which are valid ways to launch R. If its sufficient to always launch it as R then your solution is briefer but it is subsumed in the one I posted which handles it in full generality. Its really a matter of usage case at this point.

-- 
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
Received on Wed 23 May 2012 - 00:53:51 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 23 May 2012 - 10:11:48 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive