Warren  P Warren P - 2 months ago 16
R Question

Can I start an Rcmdr session from a unix shell?

I want to launch Rcmdr as a command from bash (or any unix shell), perhaps as an alias. R accepts the CMD argument and I could also pipe a script in with <. I would like the R console to stay open, and an interactive RCommander session to be started (Rcmdr is a popular GUI for R, for any newbies reading along, and it seems that you start up R, type library(Rcmdr) and then Commander() to start it up).

I am aware of how to add Rcmdr to my profile, and it appears to always start up if I include library(Rcmdr) in my .Rprofile, on my Linux workstation.

If I pipe my input in with < then this script works up to the point where it says that Commander GUI is launched only in interactive sessions:

library(Rcmdr);
Commander();


However if I run
R CMD BATCH ./rcommander.r
it just starts up and shuts down immediately, probably giving me some warning about interactive sessions that I didn't see, because CMD BATCH puts R into non-interactive mode and is thus useless for the purpose of "injecting" Rcmdr into an interactive R session.

It appears impossible to "source a file on the command line but run interactively" in R. It also appears that there are command line options to ignore the global and the user profile, but not to specify a custom profile like
R --profile-custom ./.Rprofile2


Either I would like to specify a profile that means "Right now I want to start up and use RCmdr" and still be able to run R without it sometimes.

Answer

Working on an Ubuntu machine here, I was able to use the advice provided by Dirk in this mailing list post:

nathan@nathan-laptop:~/tmp$ cat rcommander.r 
#!/bin/bash 
r -lRcmdr -e'while(TRUE) Commander();'  


nathan@nathan-laptop:~/tmp$ cat rcommander2.r 
#!/bin/bash 
Rscript --default-packages=Rcmdr -e 'while(TRUE) Commander();'

The first script uses Dirk's littler package, available on CRAN, and the second uses the standard Rscript executable. As noted, you can kill the process with ctrl + c from your terminal.