Skip to content

Wait for user input with readline()

2 messages · Nathan Miller, Joshua Wiley

#
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: