Pause R Script Until Key is Pressed in R (Example)
This article shows how to wait for a keypress in R programming.
The post consists of these content blocks:
Let’s dive into it.
Example: Specify Keypress within User-Defined Function Using readline()
This example illustrates how to insert a required keypress in a manually defined function in R.
Have a look at the following user-defined function:
fun_key <- function(x) { # Create user-defined function out <- x^2 readline(prompt = "Press [enter] to get the square of x.") out }
As you can see, we have specified the readline function and the prompt argument before the output of the function is returned.
Let’s see what happens when we apply our function:
fun_key(x = 5) # Apply user-defined function
After running the previous R code, the execution of our function is paused and the following sentence is returned to the RStudio console:
Press [enter] to get the square of x.
After pressing enter, the RStudio console returns the value 25:
# [1] 25
Video & Further Resources
Some time ago I have published a video on my YouTube channel, which explains the content of this article. You can find the video below.
The YouTube video will be added soon.
Additionally, you may read the other tutorials of my website. I have released numerous articles about execution breaks in R already:
In summary: At this point you should have learned how to pause an R script until a key is pressed in R programming. In case you have further questions, please tell me about it in the comments.
Subscribe to the Statistics Globe Newsletter
Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.
Thank you!
Welcome to the Statistics Globe newsletter. From now on, I’ll send you regular emails about statistics, data science, AI, and programming with R and Python.
I’m Joachim Schork. On this website, I provide statistics tutorials as well as code in Python and R programming.
Statistics Globe Newsletter
Get regular updates on the latest tutorials, offers & news at Statistics Globe. I hate spam & you may opt out anytime: Privacy Policy.
Thank you!
Please check your email inbox and click the confirmation link to complete your subscription. If you don’t see the email within a few minutes, please also check your spam/junk folder.






