5

I'm trying to get R to pause code execution, for the user to review some prior console output, to verify that file links have been correctly matched (using RegEx to match file names to their respective objects).

From some other answers, I've come up with:

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

As a stand alone line of code, this works as expected, but as soon as I add code below it, and send the whole block to the console, it runs right through the readline call without stopping:

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

x <- 1
y <- 2

Is there a way to get R to actually pause here?

I've also tried wrapping readline in a function, but it still doesn't work

pause <- function(){
 p <- readline(prompt="Press [enter] to continue or [esc] to exit")
 return(p)
}

pause()

x <- 1
y <- 2

Edit:

If I call the script via source(), readline does work properly. Is there a way to get that behavior without doing that?

Mako212
  • 6,787
  • 1
  • 18
  • 37
  • Take a look at the answers to this question. While the top one uses 'readline`, there are many alternate strategies suggested: https://stackoverflow.com/questions/15272916/how-to-wait-for-a-keypress-in-r/15283106 – divibisan Jun 25 '18 at 22:25

2 Answers2

2

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()
interfect
  • 2,665
  • 1
  • 20
  • 35
  • Thanks, your bolded point seems to be right on the money, describing the condition. I'll award the bounty in a day or two as long as no one else adds anything substantial. – Mako212 Jun 26 '18 at 01:52
1

Using scan would always work:

message("Press [enter] to continue or [esc] to exit")
scan(quiet = TRUE, what = "")
F. Privé
  • 11,423
  • 2
  • 27
  • 78
  • This produces the same condition as `readline` when sent to the console with additional code. The following line of code, whether it's a blank line or another line of code is taken as the input to `scan`. – Mako212 Jun 26 '18 at 15:49
  • Not when you source the file. – F. Privé Jun 26 '18 at 15:57
  • Correct, but my question is asking specifically if there is a way to replicate that behavior without calling the file with `source()`. `readline` also behaves correctly when called via `source()` – Mako212 Jun 26 '18 at 15:59