93

How can I interrupt a running script in R?

Let's say I have an infinite loop or some other problem, and I want to stop the script from running. Is there a keyboard command that does that?

If it helps any, in Python it is Ctrl + c. I tried that in R, and it didn't work.

5
  • 4
    In Rgui, if the R console has the focus, then ESC or Alt-M [Enter] or clicking on the red stop octagon will stop the curent calculation
    – Henry
    Dec 3, 2011 at 20:29
  • 1
    I think it also depends on what OS are you using (Wondows, Mac OS, Linux) and how you are running your R script (from R gui, Emacs, ...). Dec 3, 2011 at 20:31
  • 6
    ESC works in R Studio as well (when running Windows, at least). Dec 4, 2011 at 4:45
  • 2
    You should have mentioned that were on Windows in your original post. Try to mention all relevant information. You should also have mentioned how you are running R.
    – Xu Wang
    Dec 4, 2011 at 7:00
  • But then the question is how do you return to the process you interrupted? Aug 17, 2021 at 10:20

4 Answers 4

72

Self Answer (pretty much summary of other's comments and answers):

  • In RStudio, Esc works, on windows, Mac, and ubuntu (and I would guess on other linux distributions as well).

  • If the process is run in say ubuntu shell (and this is not R specific), for example using:

      Rscript my_file.R
    

    Ctrl + c kills the process

    Ctrl + z suspends the process

  • Within R shell, Ctrl + C kills helps you escape it

1
  • 23
    Unfortunately, in RStudio, Esc can't interrupt memory-intensive processes.
    – Orion
    Aug 8, 2016 at 5:28
14

Control-C works, although depending on what the process is doing it might not take right away.

If you're on a unix based system, one thing I do is control-z to go back to the command line prompt and then issue a 'kill' to the process ID.

6

Try out Ctrl + z But it will kill the process, not suspend it.

2
  • 3
    I tried Ctrl + z, but it doesn't work either. I am on Windows, btw. Thanks though.
    – Akavall
    Dec 3, 2011 at 20:15
  • Yea this is not a keyboard interrupt, might as well close the terminal window
    – altabq
    May 1, 2020 at 11:52
3

I know this is old, but I ran into the same issue. I'm on a Mac/Ubuntu and switch back and forth. What I have found is that just sending a simple interrupt signal to the main R process does exactly what you're looking for. I've ran scripts that went on for as long as 24 hours and the signal interrupt works very well. You should be able to run kill in terminal:

$ kill -2 pid

You can find the pid by running

$ps aux | grep exec/R

Not sure about Windows since I'm not ever on there, but I can't imagine there's not an option to do this as well in Command Prompt/Task Manager

Hope this helps!

Not the answer you're looking for? Browse other questions tagged or ask your own question.