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

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.