Quantcast

Wait for user input with readline()

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Wait for user input with readline()

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

 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?

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]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Wait for user input with readline()

Prof Brian Ripley
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]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
--
Brian D. Ripley,                  [hidden email]
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
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Wait for user input with readline()

Alexandre
In reply to this post by NMiller
Hi,

I have a similar problem as the one of Nate. The point is that I want to design an interactive script that need the value of two variables (x and y).

So my script as designed for the moment is :

x <- as.numeric (readline(prompt="What is the value of x? "))
y <- as.numeric (readline(prompt="What is the value of y? "))

x
y

But the problem is that if I run this script, values returned for x and y will be "NA" like you can see below :

> x <- as.numeric (readline(prompt="What is the value of x? "))
What is the value of x?
> y <- as.numeric (readline(prompt="What is the value of y? "))
What is the value of y?
> x
[1] NA
> y
[1] NA

I have no problem to understand why, because R software does not let the time to enter the value for each variable. So Nate and I want to know if there is a way, to "force" R to wait the entrance of the value of each variable like written below:

First step of the script :

> x <- as.numeric (readline(prompt="What is the value of x? "))
What is the value of x? 5

Second step of the script :

> y <- as.numeric (readline(prompt="What is the value of y? "))
What is the value of y? 9

Finally :

> x
[1] 5
> y
[1] 9

I hope that my english is not to bad and that you've understand what I mean.

Regards

Alexandre
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Wait for user input with readline()

Greg Snow-2
Part of the problem seems to be that R is set up to run in 1 of 2 modes (I may be over generalizing or over simplifying here), the modes are interactive where you type in a command, R processes it and gives results, you type in another command, etc.  The other is Batch mode where everything is processed without user interaction.

You (and others) seem to want some combination of the 2, but it is not clear exactly how to merge the 2 modes for general.  You are asking the computer to try to read your mind about when to use the next line of the script as input and when to wait for the user.

Some possibilities for you being able to tell the computer what to do:

Convert your script to a function with the readlines inside the function, then run the function from the prompt (then the readlines will expect user input).

Use tcltk (or other GUI tools) to have a separate box pop-up to get the input (this will not allow the entry to ever be automated, the script will wait until the entry is submitted).

You could combine these using if statements on the results of the interactive() function to help decide whether to pop up the boxes or not.

Hope this helps,

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
[hidden email]
801.408.8111


> -----Original Message-----
> From: [hidden email] [mailto:r-devel-bounces@r-
> project.org] On Behalf Of Alexandre
> Sent: Monday, December 06, 2010 9:26 AM
> To: [hidden email]
> Subject: Re: [Rd] Wait for user input with readline()
>
>
> Hi,
>
> I have a similar problem as the one of Nate. The point is that I want
> to
> design an interactive script that need the value of two variables (x
> and y).
>
> So my script as designed for the moment is :
>
> x <- as.numeric (readline(prompt="What is the value of x? "))
> y <- as.numeric (readline(prompt="What is the value of y? "))
>
> x
> y
>
> But the problem is that if I run this script, values returned for x and
> y
> will be "NA" like you can see below :
>
> > x <- as.numeric (readline(prompt="What is the value of x? "))
> What is the value of x?
> > y <- as.numeric (readline(prompt="What is the value of y? "))
> What is the value of y?
> > x
> [1] NA
> > y
> [1] NA
>
> I have no problem to understand why, because R software does not let
> the
> time to enter the value for each variable. So Nate and I want to know
> if
> there is a way, to "force" R to wait the entrance of the value of each
> variable like written below:
>
> First step of the script :
>
> > x <- as.numeric (readline(prompt="What is the value of x? "))
> What is the value of x? 5
>
> Second step of the script :
>
> > y <- as.numeric (readline(prompt="What is the value of y? "))
> What is the value of y? 9
>
> Finally :
>
> > x
> [1] 5
> > y
> [1] 9
>
> I hope that my english is not to bad and that you've understand what I
> mean.
>
> Regards
>
> Alexandre
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Wait-for-
> user-input-with-readline-tp3054517p3074781.html
> Sent from the R devel mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Wait for user input with readline()

Petr Savicky
In reply to this post by Alexandre
On Mon, Dec 06, 2010 at 08:25:39AM -0800, Alexandre wrote:

>
> Hi,
>
> I have a similar problem as the one of Nate. The point is that I want to
> design an interactive script that need the value of two variables (x and y).
>
> So my script as designed for the moment is :
>
> x <- as.numeric (readline(prompt="What is the value of x? "))
> y <- as.numeric (readline(prompt="What is the value of y? "))
>
> x
> y
>
> But the problem is that if I run this script, values returned for x and y
> will be "NA" like you can see below :

How do you call your code? Function readline() does not wait for
user input in a non-interactive session, for example R CMD BATCH
or Rscript.

Another situation, when readline() does not wait is, when you copy
a block of code and paste it to a running session, even if it is
interactive. If readline() is not the last line of the code, then
the next line of code is used instead of the user input.

Petr Savicky.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Wait for user input with readline()

Simon Urbanek

On Dec 7, 2010, at 12:19 PM, Petr Savicky wrote:

> On Mon, Dec 06, 2010 at 08:25:39AM -0800, Alexandre wrote:
>>
>> Hi,
>>
>> I have a similar problem as the one of Nate. The point is that I want to
>> design an interactive script that need the value of two variables (x and y).
>>
>> So my script as designed for the moment is :
>>
>> x <- as.numeric (readline(prompt="What is the value of x? "))
>> y <- as.numeric (readline(prompt="What is the value of y? "))
>>
>> x
>> y
>>
>> But the problem is that if I run this script, values returned for x and y
>> will be "NA" like you can see below :
>
> How do you call your code? Function readline() does not wait for
> user input in a non-interactive session, for example R CMD BATCH
> or Rscript.
>
> Another situation, when readline() does not wait is, when you copy
> a block of code and paste it to a running session, even if it is
> interactive. If readline() is not the last line of the code, then
> the next line of code is used instead of the user input.
>

You can't tell R code and input apart in cases like
R < myScript.R
but you can if you use source() or Rscript -- and you can treat them differently.

I assume what Alexandre wants is something like:
input=file("stdin")
print(readLines(input,1))
which works both in interactive and non-interactive setting and expects input in both cases (if you use Rscript or R -e 'source("...")' ).

Cheers,
Simon

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Wait for user input with readline()

Alexandre
Thanks all,

Even if I'm not an expert in programmation, I'll try to compile informations and try to sort my problem.

Alexandre
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Wait for user input with readline()

jcharlto
This post has NOT been accepted by the mailing list yet.
THIS IS A HOW-TO on HOW TO SCRIPT OR PROGRAM IN THE R-PROGRAMMING LANGUAGE A SIMPLE INPUT AND OUTPUT OF NUMERIC INFORMATION OR DATA.  In other words, you are writing a program, which you want to save, and then later run from the console by using some Source("My File") type deal.  The program will ask you for numeric information, you will put in numeric data, and assuming you ask it to output (demonstrated), you will get the numeric data output.

1.  Think of a name.  You're going to call a function this name.  The name I'm going to use is "BOB" (you won't use the quotations) such that  you will write on your first line of code

BOB <- function()

2.  On the second line, write and open bracket "{" so that now you have 2 lines of code that look like this:

BOB <- function()
{


3.  On the third line, write "as.numeric(readline("Please enter a number:>>> "))", so that your 3 lines of code look like

BOB <- function()
{
as.numeric(readline("Please enter a number:>>> "))


4.  On the fourth line, write and close bracket "}" so that now you have 4 lines of code that look like this:

BOB <- function()
{
as.numeric(readline("Please enter a number:>>> "))
}

5.  Create a name for the user input, like "Input_BOB" and assign it the function BOB (when you wrote the first line of code, BOB was given "()" parenthesis because you're calling BOB a function) Your 5th line reads:

BOB <- function()
{
as.numeric(readline("Please enter a number:>>> "))
}
Input_BOB <- BOB()


6.  Now you can do anything numerical you want with Input_BOB (i.e. x <- Input_Bob*5, which is understood as "x equals or is assigned Input_BOB times Five).  You can multiply numbers by it, you can print it (or read it on the console) by writing print(Input_BOB).  Using this last example, printing Input_BOB, your code looks like this.

BOB <- function()
{
as.numeric(readline("Please enter a number:>>> "))
}
Input_BOB <- BOB()
print(Input_BOB)


7.  Save this as Bob in your working directory.  (If you want to know what that is, in the R-console type getwd()).  Let's say we called this simple program, Program_Bob.

8.  In the R-Console type:
> source("Program_Bob.R")

9.  You will see:
Please enter a number:>>>

10.  Enter any number, let's say 5. and press Enter.

11.  Lo and Behold, you will see:
[1]   5

AMAZING.  So Simple, but honestly one of the hardest solutions on the web to find.  I just started R, 8 hours ago.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Wait for user input with readline()

torekp
jcharlto,

Thank you thank you thank you, you just saved me about 7.9 hours!
Loading...