How to Wait for a Keypress in R

How to wait for a keypress in R?

As someone already wrote in a comment, you don't have to use the cat before readline(). Simply write:

readline(prompt="Press [enter] to continue")

If you don't want to assign it to a variable and don't want a return printed in the console, wrap the readline() in an invisible():

invisible(readline(prompt="Press [enter] to continue"))

How can code execution be paused until a key is pressed?

By "send the whole block to the console", it sounds like you are copy-pasting your code into a running R instance.

If you do that, R will run each line in the order it receives it, as soon as it receives it. If any of the lines tries to take input, that input will come from whatever you are copy-pasting. So if you copy-paste this:

readline(prompt="Press [enter] to continue or [esc] to exit")

x <- 1
y <- 2

R will read and run the first line first. That first line will run and read the next line of input (which is a blank line here). Then R will come back and read and execute the other two lines.

You need to get your code entered into R completely before any of it runs. Try wrapping the whole thing in a function:

dostuff <- function(){
readline(prompt="Press [enter] to continue or [esc] to exit")
x <- 1
y <- 2
}
dostuff()

Make readline wait for input in R

If you want to do this in interactive mode then you already have answers but not for use with Rscript. For that instance you need to send messages to the console with cat:

If this test file is named 'prompt.r' and is in the directory where you are working in a system console session:

cat("a string please: ");
a <- readLines("stdin",n=1);
cat("You entered")
str(a);
cat( "\n" )

Then you can run it from the command line as

$ Rscript prompt.r

If you want an all-purpose script then this would run your script under interactive conditions and my script for non-interactive ones:

if (interactive() ){raw <- 
readline("TRUE or FALSE -- this is a validation run: ")

if (raw == "F" | raw == "FALSE" | raw == "False"){
validation <- F
} else{
validation <- T
}
rm(raw) } else{
# non-interactive
cat("a string please: ");
a <- readLines("stdin",n=1);
cat("You entered")
str(a);
cat( "\n" )}

Time user input from first keystroke in R

I recently solved this issue using a combination of two packages - tictoc and keypress.

From keypress you can use the keypress() function that waits for user input and then outputs the key that was pressed - keypress() only works in R on command line and it supports the majority of keys, but not all of them.

To time from a keypress you can write a simple if statement that calls the tic() function from tictoc.

Example

When I posted this question I was looking to time how long it would take someone to type the entire lowercase alphabet after hitting the 'a' key.

require(tictoc)
require(keypress)

for(i in 1:26){

a=keypress()

if(a==letters[1]) tic()
if(a==letters[26]) toc()

cat(paste(a))
}

abcdefghijklmnopqrstuvwxyz6.649 sec elapsed

This starts the stopwatch when 'a' is hit, and then stops it when the 'z' key is pressed.

How do I make an r-script wait for a mouse click?

While I didn't find an answer to make the script wait until the user clicks with the mouse, at least I can wait for him to press enter on the console:

readLines('stdin', n=1)

How to wait for user to input values in combo box in gWidgets2RGtk2?

The handler function is called when an item is selected in the combo box. It is the place to put your actions:

addHandlerChanged(op1, handler=function(...){
if (svalue(op1)=="Math"){
source("Rscript1")
}else if (svalue(op1)=="Science"){
source("Rscript2")
}else{
source("Rscript3")
}
})


Related Topics



Leave a reply



Submit