Akavall
Akavall

Reputation: 86228

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

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.

Upvotes: 96

Views: 367613

Answers (4)

Akavall
Akavall

Reputation: 86228

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

Upvotes: 77

Mgarvey
Mgarvey

Reputation: 91

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!

Upvotes: 3

Sachin
Sachin

Reputation: 18747

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

Upvotes: 6

geoffjentry
geoffjentry

Reputation: 4754

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.

Upvotes: 14

Related Questions