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 www.statisticsglobe.com.

 

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.


6 Comments. Leave new

  • Hi, Thanks for the post. I have a query, more from a mathematical perspective. I am running a metropolis hasting, which is taking quite a time. If I interrupt the process in between to check the result and then continue, vis a vis if I run the MH at a go , will there be any significant difference in the result

    Reply
    • Hey Arnab,

      Please excuse the late response. I was on a long holiday so unfortunately I wasn’t able to reply sooner. Still need help with your code?

      Regards,
      Joachim

      Reply
  • Yeah , I am still curious whether Statistically any issue arise if I interrupt Meteropolis Hasting loop any between

    Reply
    • Hi Arnab,

      I’m not an expert on the metropolis hasting algorithm. However, I assume this depends on how you have programmed your loop. Could you please share your code and illustrate the structure of your data?

      Regards,
      Joachim

      Reply
  • hello dear, what is the solution for the “Interrupt” during data analysis by R

    Reply

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.

Top