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

Duncan Murdoch murdoch.duncan at gmail.com
Sun Apr 7 13:34:57 CEST 2013


On 12-11-01 4:14 AM, Jan van der Laan wrote:
>
> 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?

Yes, don't use a script, use a package.  Packages can run startup code 
when they load.  Then include your package among those loaded by 
default.  See the ?options help on defaultPackages for how to do that 
via environment variable.

Duncan Murdoch



More information about the R-help mailing list