Re: [Rd] Wait for user input with readline()

From: Prof Brian Ripley <>
Date: Tue, 23 Nov 2010 12:32:29 +0000 (GMT)

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]]
> ______________________________________________
> mailing list

Brian D. Ripley,        
Professor of Applied Statistics,
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________ mailing list

Received on Tue 23 Nov 2010 - 12:38:32 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 23 Nov 2010 - 14:30:25 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive