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]]
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
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.
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.
jcharlto, Thank you thank you thank you, you just saved me about 7.9 hours! -- View this message in context: http://r.789695.n4.nabble.com/Wait-for-user-input-with-readline-tp3054517p4646826.html Sent from the R devel mailing list archive at Nabble.com.