How can I interrupt a running code in R with a keyboard command?

R

R Problem Overview


Can anyone tell 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 :).

I did some search for this seemingly simple question, but could not find the answer.

Thanks in advance.

R Solutions


Solution 1 - R

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 ran 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

Solution 2 - R

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.

Solution 3 - R

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

Solution 4 - R

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!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAkavallView Question on Stackoverflow
Solution 1 - RAkavallView Answer on Stackoverflow
Solution 2 - RgeoffjentryView Answer on Stackoverflow
Solution 3 - RBhushanView Answer on Stackoverflow
Solution 4 - RMgarveyView Answer on Stackoverflow