How to Stop Running R Code With the Keyboard or Mouse
Most of us make mistakes and encounter syntax errors when writing codes. We can correct some of these immediately after executing the code.
This article is about stopping running R code in unexpected situations. It differs from enabling or inserting a planned pause in code execution for debugging purposes.
When we stop the running of R code, the execution of the code stops midway. We cannot resume execution midway and must re-run the code after modifying it or enabling/inserting planned pauses using various debugging features.
Use Keyboard Shortcut to Stop Running R Code
Executing R code can sometimes lead to lengthy computations or unintended infinite loops. In such situations, it’s important to know how to stop the execution of your R code.
The method to stop running code with a keyboard command depends on whether we use the R command line or a graphical user interface.
- When running code in
RStudio
, press the Esc key. It also works in R’s graphical interfaces, namely,Rgui
for Windows andR.APP
for Mac. - Press Ctrl+C when running code using the R command-line interface.
The Ctrl + C
keyboard shortcut is a reliable tool to stop running R code, helping you regain control of your R session and prevent unwanted outcomes.
Use Mouse Action to Stop Running R Code
Using the mouse to halt code execution is a convenient and user-friendly option. The options to stop running code with the mouse will depend on the software used.
We will consider the options available in RStudio
. It is available for Windows, Mac, and Linux.
If you’re using RStudio
, you can stop code execution using the mouse. Follow the steps below:
-
Write or run your R code in the
RStudio
script editor. -
While the code is running, look for the
"Stop"
button in the Console pane. A red square represents it. -
Click the
"Stop"
button to interrupt the code execution.
Here’s an example of how to use the "Stop"
button in RStudio
:
# Simulating a time-consuming operation
for (i in 1:1000000) {
sqrt(i)
}
While this code is running, click the "Stop"
button in the Console pane. RStudio
will halt the execution of the code.
The "Stop"
button, found in RStudio
, provides a simple and user-friendly way to pause R code execution. This feature enables users to take command of their R sessions and prevent potential challenges stemming from code that runs for an extended period or contains errors.
Knowing when to use the keyboard shortcut or the mouse to stop code execution depends on your preference and the specific context.
-
Keyboard Shortcut (
Ctrl + C
): This method is handy when working in the R console or another terminal-based environment. It’s quick and universally applicable. -
Mouse Action (
"Stop"
Button): If you’re usingRStudio
and working with scripts or running code from a script file, the"Stop"
button in the Console pane offers a user-friendly way to halt code execution. It’s especially useful when your R code runs within a script that’s not in the interactive console.