[R] Standard input and R

Jon Clayden j.clayden at ucl.ac.uk
Wed Dec 19 15:32:28 CET 2007

Dear all,

I am trying to wrap a *nix shell script around R for a particular  
purpose, for which I need to get R to execute predetermined commands  
but retain interactivity and allow user input during their execution.  
A straight redirection of standard input is therefore not appropriate,  
and I don't think "littler" is the solution because I don't want to  
write an independent R script.

What I want is effectively something like what ruby does with the '-e'  

$ ruby -e 'gets'
<waits for input from terminal>

R seems to accept an '-e' option too, even though I can't see it  
mentioned in the output from 'R -h', but it doesn't work the same:

$ R --slave --vanilla -e 'scan(what=character(0),nlines=1,quiet=T)'

The only way I can get this to work is by creating an .Rprofile file  
containing the commands I want to run, but this is suboptimal for a  
number of reasons: I have difficulties if an .Rprofile already exists  
in the working directory, the default packages are not loaded, and the  
process doesn't quit if stop() is called.

I have a vague inkling that this may be achievable with a  
pseudoterminal (pty) or named pipe, but if anyone knows a simpler  
route I'd be glad to avoid getting involved in that kind of thing...

Many thanks in advance.

More information about the R-help mailing list