[R] Wait for user input with readline()
Joshua Wiley
jwiley.psych at gmail.com
Mon Nov 22 20:55:10 CET 2010
Hi Nate,
There may be better ways, but on the couple instances I've wanted to
wait for keyboard input I used this type of paradigm:
foo <- function() {
x <- 1:10
y <- rnorm(10)
input <- NA
while(!isTRUE(input == "1") && !isTRUE(input == "2")) {
cat("Please type '1' if you want the first variable on the x
axis and '2' if you want the second.", fill = TRUE)
input <- scan("", what = "character")
if(input == "1") {
plot(x, y)
} else if (input == "2") {
plot(y, x)
} else {cat("Sorry, I didn't catch that", fill = TRUE)}
}
}
Perhaps it will be of some use to you.
Best regards,
Josh
On Mon, Nov 22, 2010 at 11:13 AM, Nathan Miller <natemiller77 at gmail.com> wrote:
> Hello,
>
> I am trying write a script that includes a prompt for user input using
> readlines(). 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?
>
> Thanks for your help,
>
> Nate
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/
More information about the R-help
mailing list