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); a1: 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.krishnan at gmail.com)
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.> 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
-----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: bernd at t40:~/r-test $ cat test.R readline2 = function(prompt=""){ cat(prompt) readLines("stdin", 1) } print(readline2("enter a number: ")) bernd at 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-----
Possibly Parallel Threads
- Issue with %in% - not matching identical rows in data frames
- Referencing factors through their equivalent numeric level
- (PR#1964) The attached function working fine with R 1.3.0 but giving problem with R 1.5.1 (PR#1964)
- Pull data from Tally 9.1 to R studio
- How to use kerberos as the default auth in AD config?