Stop Running R Code with Keyboard Shortcut (Example)


In this tutorial you’ll learn how to interrupt a currently running R script using a keyboard shortcut in R.

Table of contents:

So now the part you have been waiting for – the exemplifying R syntax!


Example: Keyboard Shortcut to Stop Running R Script in RStudio

The shortcut to interrupt a running process in R depends on the R software and the operating system you are using.

However, if you are using RStudio on a Windows computer, you can usually use Esc to stop a currently executing R script.

This example illustrates how to do this in practice. Let’s assume that we are running a time-consuming for-loop with many iterations:

for(i in 1:2) {        # Run endless for-loop
  print(i)             # Print some output
  Sys.sleep(5)         # Make for-loop even slower
# [1] 1
# [1] 2
# [1] 3

Then, we can press Esc to interrupt the loop. As you can see based on the previous output of the RStudio console, in this example we pressed Esc after approx. 11 seconds.

Please note that the Esc command might not work in case you are running a memory-intensive process. Have a look at this thread on Stack Overflow to get more tips on how to stop memory-intensive processes. In this thread, you’ll also find explanations for other operating systems such as Mac OS, Linux, and Ubuntu.


Video, Further Resources & Summary

Do you need more explanations on the R programming syntax of this post? Then you might want to watch the following video of my YouTube channel. I illustrate the R code of this article in the video.


The YouTube video will be added soon.


Additionally, you may want to read the related articles on


This article explained how to use keyboard shortcuts to stop executing an R code in R. In case you have additional questions, don’t hesitate to let me know in the comments section below.


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.

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