Stopping the Script Until a Value Is Entred from Keyboard 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"))

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" )}

How do I stop/end/halt a script in R?

As far as I could find, there is no single command that really stops a script on every platform/version. There are several ways to handle this:

Put it in a function or curly brackets:

{
if (TRUE) {stop("The value is TRUE, so the script must end here")}

print("Script did NOT end!")
}

OR evaluate the error and handle it like in an if else construction:

if (TRUE) {stop("The value is TRUE, so the script must end here")    
} else { #continue the script
print("Script did NOT end!")
}

OR (EDIT):
Another possibility is to call the script from a seperate 'main' R-scipt, with source("MyScript.R"). Then the script terminates. This however, suppresses all output other then errors to the console.

OR for more complex operations, use tryCatch() as shown here

R: I fail to pause my code

The problem with readline is that if you paste your script into an R console, or execute it from eg Rstudio, the redline function is read and then the next line of the script is read in as the console entry, which in your case sets the value of something to print('B).

An easy way to get around this is to stick your entire code in a function, then call the function to run it. So, in your case:

myscript = function(){

print('A')
something = readline(prompt = "Press Enter")
print('B')
print('C')

}

myscript()

The output of this for me (in Rstudio, with R version 3.1.1):

[1] "A"
Press Enter
[1] "B"
[1] "C"

This has always felt like a bit of a hack to me, but it's essentially what the readline documentation recommends in its example.

I've never used sleep in my code, so I can't help you there.

Edit to clarify based on comments: This will only work if myscript() is the very last line of your script, or if it is manually entered into the console after running the script to generate the function. Otherwise, you will run into the same problem as before- the next line of code will be automatically entered.

How to prompt the user for input from the console?

Readline does wait for the user's input. But the problem is that, in your case, when you run all of your code at once, R will treat 'n2<-readline(prompt="Enter skip 2: " )' as the user's input (it indeed is) to the first readline. This is the root of the problem.

So basically you have two choices to deal with your problem, the first is to find some specific package in R that halts R execution process when prompting; the second one is to stick to the R base, but only use readline in the ending block of your file. In your case, you can use a little trick like this:

{
n1<-readline(prompt="Enter skip 1: " )
n2<-readline(prompt="Enter skip 2: " )
n1<-as.integer(n1)
n2<-as.integer(n2)
}

The rationale behind this trick is this: when R read the thing between { and }, it will treat all of the things in between as a whole command, this is the ending block of the code, so it behaves like what you want. R will not feed part of this command to another part of the command, thus prevents the problem you met.

You can develop similar tricks yourself, like putting the readline in a function:

foo <- function(){
n1<-readline(prompt="Enter skip 1: " )
n2<-readline(prompt="Enter skip 2: " )
n1<-as.integer(n1)
n2<-as.integer(n2)
c(n1, n2)
}

foo()

In this case, the whole foo function will act as the ending block of your code, and all things work as you want. Hope this helps.



Related Topics



Leave a reply



Submit