[R] Command-line arguments and --interactive

Duncan Murdoch murdoch at stats.uwo.ca
Tue Nov 10 12:18:03 CET 2009


Adam D. I. Kramer wrote:
> Hello,
>
>  	I am interested in passing a command or two to R on the command
> line. The desired behavior is for R to run these commands first, and then
> begin an interactive session. For example:
>
> $ R -e 'foo <- read.csv("/tmp/foo.csv")'
> ...which would launch R and execute that command, so when I return from
> getting coffee, foo would be loaded.
>
> I know that if I put this command into my .Rprofile, it will execute as
> desired, but I'm looking for something a bit more modular.
>
> I have tried the following:
>
> $ echo '10*5' | R
> ...fails unless I specify --save --no-save or --vanilla. I choose the
> latter; R prints 50 and quits...but I do not want it to quit!
>
> $ echo '10*5' | R --interactive
>
> ...fails in an interesting way: I get an infinite loop of Save workspace
> image? [y/n/c]: Save workspace image? [y/n/c]:, etc. I have to kill R in
> another window. This also happens with
> $ echo '10*5; scan()' | R --interactive
>
> ...even though I'd expect scan() to prompt for input. If I specify
>
> $ echo '10*5; scan()' | R --interactive --vanilla
>
> ...then R just prints 50 and quits. Is this a bug with "interactive?" The
> description of "Force an interactive session" is not informative enough for
> me to have any further guess as to what to do here, but my expectation is
> that it would make R not auto-quit.
>
> ...perhaps I don't know how to use --interactive properly? Could somebody
> point me on the right track? Googling for "R --interactive" is nigh untu
> useless. :-\
>
> This behavior is present in R 2.9.2 and 2.10.0.
>   

--interactive tells R that there is a human producing the input stream, 
so it can ask questions and expect them to be answered.  In your 
experiments with it, your input stream was the pipe holding the output 
of echo, and R got confused because that pipe wouldn't answer its question.

Your problem is that you want an input stream that starts out from your 
fixed code and then switches to your shell's stdin.  I think you can do 
that on some systems
by saving your input to a file then concatenating it to stdin, e.g. 
something like this:

echo '10*5; scan()' >test.R
cat test.R - | R --interactive

I don't know if there's a way to do this in one line, and I'd expect 
some oddities.

Duncan Murdoch

> Many thanks!
>
> Cordially,
> Adam Kramer
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>




More information about the R-help mailing list