[Rd] Wait for user input with readline()
Greg Snow
Greg.Snow at imail.org
Tue Dec 7 17:48:40 CET 2010
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
greg.snow at imail.org
801.408.8111
> -----Original Message-----
> From: r-devel-bounces at r-project.org [mailto:r-devel-bounces at r-
> project.org] On Behalf Of Alexandre
> Sent: Monday, December 06, 2010 9:26 AM
> To: r-devel at r-project.org
> 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.
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
More information about the R-devel
mailing list