weblover
weblover

Reputation: 371

stopping the script until a value is entred from keyboard in R

i have a script , and i want to start it with a keyboard input and then continue the work based on the value entered in this variable , i tried y=readline("please enter a value") but the script won't wait to enter a value , it just display this sentence and continue , how this can be done ??? thanks in advance ??

Upvotes: 3

Views: 5987

Answers (3)

arielf
arielf

Reputation: 5952

I had the same problem on Ubuntu (not Windows) and found a solution.

Instead of Rscript use littler (/usr/bin/r) and make sure to pass the interactive flag -i to littler. This combination manages to convince readline() to work as desired in a script:

#!/usr/bin/r -vi

eprintf <- function(...) cat(sprintf(...), sep='', file=stderr())

prompt.read <- function(prompt="\n[hit enter to continue]: ") {
    eprintf("%s", prompt)
    invisible(readline())
}

ans <- prompt.read('Please enter a value: ')
eprintf("You have entered: '%s'\n", ans)

# rest of script comes here...

When I run it as a script, I get:

$ ./rl.r
Please enter a value: 42
You have entered: '42'

$ ./rl.r
Please enter a value: Hello to you!
You have entered: 'Hello to you!'

To install littler (on Ubuntu):

sudo apt-get install littler

Upvotes: 0

Gavin Simpson
Gavin Simpson

Reputation: 174813

Here is a very simple #! script that creates a function foo() whose sole purpose is to echo back out it's argument 'bar'.

#! /home/gavin/R/2.13-patched/build/bin/Rscript --vanilla
foo <- function(bar) {
    writeLines(paste("You supplied the following information:", bar))
}

## grab command arguments passed as -args
args <- commandArgs(TRUE)

## run foo
foo(args)

We grab any command line arguments passed to the script from the shell using the commandArgs() function, and then pass them to foo() with the last line of the script.

If we have that bit of code in file foobar.R, then we can pass in an argument and run the script using the Rscript interface. You also need to make the above executable (chmod it).

Then the script can be called like and works as follows:

[gavin@desktop ~]$ ./foobar.R Cl
You supplied the following information: Cl

But do note the information in ?Rscript as unfortunately, standard Windows cmd shells don't know about #! like scripts, so you might need to install some other shell (the help suggests a Cygwin shell should work) to use the functionality I show.

Update: Using source() and readline().

An alternative, if you can do without having to run non-interactively (i.e. you don't min opening the R GUI and running one line of code), is to just source() the script. For example, if this was in a script call barfoo.R:

dynamicwilcox <- function() {
    ANSWER <- readline("What column do you want to work on? ")
    if(ANSWER=="Ph") {
        writeLines("column was 'Ph'")
    } else if(ANSWER=="Cl") {
        writeLines("column was 'Cl'")
    } else {
        writeLines(paste("Sorry, we don't know what to do with column", ANSWER))
    }
    ANSWER ## return something
}

dynamicwilcox()

Then from the R Gui prompt, we could do:

R> source("barfoo.R")
What column do you want to work on? Cl
column was 'Cl'

or if you don't want to specify the full path do this:

R> source(file.choose())

readline() works just fine when used in an interactive R session and is really the best tool for the job - this is exactly what it was deigned to do.

The whole premise that you want to run a script in a batch mode but provide some input doesn't make much sense. R expects scripts to be self contained when run in batch mode. You might not realise it, but when you double click your script, it is being run in batch mode.

Upvotes: 6

mdsumner
mdsumner

Reputation: 29477

You probably want scan(), something like:

print("please enter a value")
y <- scan(file = "", what = "", nmax = 1)

scan() will wait for the user to enter and any text will be stored in y - a vector of mode character in this case.

Upvotes: 2

Related Questions