[R] User input when running R code in batch mode

Kaushik Krishnan kaushik.s.krishnan at gmail.com
Tue Oct 27 09:20:38 CET 2009


I've been stumbling over a simple issue that undoubtedly has an easy
solution.  I need to have some way for a user to enter some values
into a data frame which R will then work on.  I know that data entry
should ideally be done otherwise and I should use R only for the
computation, but R's data manipulation abilities makes it efficient
for me to write the entire code in it.

When I ran R line-by-line, I did:
> a <- scan (what='character',n=1)
1: abcd
Read 1 item
> a
[1] "abcd"
Everything works fine.

But if I save the line "a <- scan (what='character', n=1); a" as
`test.r' and run the file in command line, I get:
$ r --vanilla < test.r
> a <- scan(what='character',n=1); a
1: Read 0 items
Now it's not working.

I read what others had suggested on earlier threads and tried
different functions, including readLines().  In line-by-line, I wrote
the same lines as earlier, except using `readLines(con=stdin(),n=1)'
instead of `scan(what='character', n=1)'.  It worked fine.

Yet, when I tried the modified lines (with readLines() instead of
scan()) in batch mode, R didn't wait for user input and said

Is there any way to make R stop for the user to enter values when
running in batch mode either by changing the way I invoke scan() or
readLines() or by using any other function?

Thanks in advance
Kaushik Krishnan
(kaushik.s.krishnan at gmail.com)

More information about the R-help mailing list