HOWTO · R
How to Stop Running R Code With the Keyboard or Mouse
This article shows how to stop running R code with the keyboard or mouse.
On this page
To stop a long-running or unintended R computation, interrupt its current execution with the control for your interface. Stopping is different from adding a planned pause for debugging: the interrupted run does not resume from the same point. After you stop it, review any work already completed and run the needed code again, making changes first if necessary.
Interrupting a command does not roll back actions it completed before the stop, so inspect changed objects or files before repeating a command that modifies them. The R session itself remains the place to check what completed and decide which commands to run next. In the R command-line interface, press Ctrl+C to interrupt the active command.
Stop Running R Code With a Keyboard Shortcut
The keyboard shortcut depends on where R is running. In RStudio, press Esc while the computation is active. The same key is used in the graphical interfaces Rgui on Windows and R.APP on Mac. If you are using the R command-line interface in a terminal, press Ctrl+C to interrupt the current computation. These directions apply to the named interfaces; do not assume the same key is handled identically by every editor or terminal.
A keyboard shortcut is useful when the R console has focus and you can type into it. If the application does not respond immediately, give it a moment to process the interrupt. A computation may need to reach an interruptible point before control returns. Once it does, check the console for the prompt and any message, then decide whether to correct and rerun the code.
Stop Running R Code With the RStudio Stop Button
In RStudio, you can interrupt a running command with the mouse. Run code from the script editor or Console, then look in the Console pane while it is active. The Stop button appears there with a red-square cue. Click it once to interrupt the current execution. This is a convenient option when the mouse is already in use or the keyboard shortcut is unfamiliar.
Run this short example in an interactive R session. It waits briefly, giving you time to try the control:
Sys.sleep(5)
The call waits for five seconds and produces no printed value by itself. When it is running in RStudio, click the Console pane’s Stop button; with the R command line, use Ctrl+C. The example is intentionally short, so it also finishes on its own if you do not interrupt it. A longer operation can be interrupted in the same way, subject to the interface responding to the request.
Choose the keyboard control for your active R interface: Esc for RStudio, Rgui, or R.APP, and Ctrl+C for the R command-line interface. Use the RStudio Stop button when you prefer a mouse action. Either control stops the current run; it does not insert a debugging pause or preserve a place from which execution can continue. Rerun the required commands after the interruption, because statements later in the stopped run have not completed.