[R] Start R from bash/bat file and end in interactive mode

Jan van der Laan rhelp at eoos.dds.nl
Thu Nov 1 09:14:09 CET 2012


I have a r-script (rook.R) that starts a Rook server. To present users  
from having to start R and type in source("rook.R"), I want to create  
a bash script and bat file that starts R and sources the script.  
However, to keep the Rook server running R should not close after  
running the script and stay in interactive mode. This proves more  
difficult than expected.

I tried various combinations of commandline parameters and pipes, but  
none of them seem to work:

$R -f rook.R --interactive
Runs and quits

$ cat rook.R | R
Fatal error: you must specify '--save', '--no-save' or '--vanilla'

$cat rook.R | R --no-save
Runs and quits

$R --no-save < rook.R
Runs and quits

$R --no-save --interactive < rook.R
Runs and quits

I would have expected the --interactive parameter to do what I want,  
but it doesn't seem to do anything.

What does work is to create a .Rprofile with sourc("rook.R") in it in  
the directory and then start R (just plain R). However I don't find  
this a very elegant solution. I could of create the .Rprofile file in  
the bash script which is somewhat better, but still not very elegant.  
I end up with the following bash script:

#!/bin/bash
echo "source(\"rook.R\")" > .Rprofile
R

Another, not very elegant, possible solution which I haven't tried is  
to start a while loop at the end of the script with a sleep command in  
it.

Does there exist a better solution?

Regards,

Jan



More information about the R-help mailing list