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

Martin Maechler maechler at stat.math.ethz.ch
Tue Oct 27 19:23:40 CET 2009


>>>>> Barry Rowlingson <b.rowlingson at lancaster.ac.uk>
>>>>>     on Tue, 27 Oct 2009 10:17:24 +0100 writes:

    > On Tue, Oct 27, 2009 at 9:20 AM, Kaushik Krishnan
    > <kaushik.s.krishnan at gmail.com> wrote:

    >> $ r --vanilla < test.r
    >>> a <- scan(what='character',n=1); a
    >> 1: Read 0 items
    >> character(0)
    >> ----
    >> Now it's not working.

    > Assuming this is a unix environment, the syntax '< test.r' means 'my
    > standard input stream is the file test.r'. That's not what you want.
    > Give R the file name as an argument and let the standard input stream
    > remain user input:

    > $ r --vanilla  test.r
    > 1: hello
    > Read 1 item
    > [1] "hello"

    > Note that this is 'r' and not 'R'. For me this comes from the
    > 'littler' package in Ubuntu Linux. The same thing with 'R' doesn't
    > work:

    > $ R --vanilla  test.r
    > ARGUMENT 'test.r' __ignored__
    > [banner]
    >> [the R prompt appears]



    >  Maybe there's a way of doing this with big R, but I think
    > littler is designed for this kind of thing.

yes, and it was historically the first, and maybe still the most efficient
way to do so.
The "big R" way is to use  'Rscript'  which comes with R,
e.g.,

 Rscript --vanilla -e 'cat("a string please: "); a <- readLines("stdin",n=1); str(a)' 
input a string please: Foobar
 chr "Foobar"

(Note that here I use  -e '<expression>'  instead of an R script file
 which of course is possible too).

Martin Maechler, ETH Zurich

     >> 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?

    >  An alternative is to use the tcltk package to make a
    > dialog for user input.

    > Barry

    > ______________________________________________
    > 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