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

2009-10-27 Thread Kaushik Krishnan
Hi

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
character(0)

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
`character(0)'.

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.krish...@gmail.com)

__
R-help@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.


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

2009-10-27 Thread Barry Rowlingson
On Tue, Oct 27, 2009 at 9:20 AM, Kaushik Krishnan
kaushik.s.krish...@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.

 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@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.


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

2009-10-27 Thread Bernd Kreuss
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Kaushik Krishnan wrote:
 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?

At least on linux this works:

be...@t40:~/r-test $ cat test.R

readline2 = function(prompt=){
cat(prompt)
readLines(stdin, 1)
}

print(readline2(enter a number: ))

be...@t40:~/r-test $ Rscript test.R
enter a number: 42
[1] 42


-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iD8DBQFK5xUSxT6R4jlFoh0RAkhTAKCB4qUjPlULL9vjUaLLxarTor2z1QCeNGHA
4B2VmkqtyEKMKKGUnt8sjiY=
=VYJs
-END PGP SIGNATURE-

__
R-help@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.


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

2009-10-27 Thread Martin Maechler
 Barry Rowlingson b.rowling...@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.krish...@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@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.

__
R-help@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.