[Rd] Wait for user input with readline()

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Nov 23 13:32:29 CET 2010


What you have not done is told us how you run the script.

On Mon, 22 Nov 2010, Nathan Miller wrote:

> Hello,
>
> I am trying write a script that includes a prompt for user input using
> readlines() and was told that folks at R-devel might be able to help.

(But this is an R-help question.)

readline() and readLines() are separate functions: there is no 
readlines().

Your script is very far from self-contained, and you haven't told us 
how you run it.   Here's a working example:

toucan% cat foo.R
cat("What is the O2 saturation in umole/L? ")
foo <- scan("stdin", 0,  n=1, quiet = TRUE)
print (foo)

toucan% Rscript foo.R
What is the O2 saturation in umole/L? 45
[1] 45


> I am running into the problem that when I run readlines() as a single line
> the prompt works perfectly, but when I try to run a block of code which
> includes the readline function, the script doesn't wait for the user input.
> I have seen this question posted before when I did a search, but I didn't
> find an suitable answer. Is there a means of ensuring that the script does
> not proceed until a value has been entered to readline(). Can I put readline
> in a function that will wait for input?

But this is all based on a false assumption: ?readline says

      In non-interactive use the result is as if the response was RETURN
      and the value is ‘""’.

So you cannot usefully use readline() in what most people understand 
by 'running a script', as that is 'non-interactive use'.

> Are there other options for getting user input that allow require that the
> script wait for user input?
>
> In the code I want the user to input the value for O2sat using
> O2sat=as.numeric(readline("What is the O2 saturation in umole/L?"))
>
> The problem is that when I run this entire block of code rather than running
> individual functions, the program doesn't wait for the user input and just
> continues to the next line of code. Does anyone have a work around for this
> type of problem?
>
> Thanks for your help,
>
> Nate
>
> #################
>
>
> O2sat=as.numeric(readline("What is the O2 saturation in umole/L?"))
>
> r=function(x) {
>        deltaO2=meanblank-q[2]
>        umoleO2=(deltaO2/100)*O2sat
>    }
>
> umoleO2perL=r(minO2mean)
> umolesO2consumed=umoleO2perL*Vial_vol
> names(umoleO2consumed)="umoleO2consumed"
> O2consumed=data.frame(filenames,umoleO2consumed)
> O2consumed
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


More information about the R-devel mailing list