How to Restart R in RStudio

Jesse John Feb 15, 2024
  1. The Need to Restart R
  2. Use the Ctrl + Shift + F10 Shortcut to Restart R Interactively in RStudio
  3. Restart R in Code (Undocumented)
  4. Use R Projects
  5. Using the restartSession Function to Restart R in RStudio
  6. Using the quit Function
  7. Conclusion
How to Restart R in RStudio

RStudio is a widely used integrated development environment (IDE) for R, offering a range of tools and features to streamline the R programming experience. Occasionally, there may be situations where you need to restart your R session to clear the workspace or resolve issues.

In this article, we will explore different methods to restart R within RStudio, providing detailed examples for each method. We will explain the need to restart R and then mention alternate ways to restart R using RStudio.

The Need to Restart R

When we do any data analysis, we must ensure that our results depend only on the data we use for the analysis. However, by default, RStudio gives us one common R process and a common global environment for all the files that are open in RStudio.

If we create a variable with the same name in multiple documents, the one created last overwrites the previous one. If we go back to the first document and use the variable, we get its new value created in the second document.

To load all required libraries and create all variables in the current document, we need to restart R when starting work on a new document or switching back to a document. Restarting R will clear all variables and unload all our loaded libraries.

An additional benefit of restarting R is that it frees R’s computer memory, which is no longer in use.

Use the Ctrl + Shift + F10 Shortcut to Restart R Interactively in RStudio

RStudio offers a keyboard shortcut for restarting R, providing a quick and convenient option for users who prefer using shortcuts. This is the preferred method among all in this article.

Within RStudio, we can restart R using either of the two options: menu item or the keyboard shortcut.

  1. Click Session > Restart R.
  2. Press Ctrl + Shift + F10. On Mac, press Command in place of Ctrl.

This method doesn’t involve writing any code explicitly. Instead, it’s a manual process through the RStudio interface.

This is the only documented way to restart R from the RStudio IDE.

restart r shortcut

Restart R in Code (Undocumented)

An undocumented way to restart R in RStudio mentioned in online forums/articles is using the code below:

.rs.restartR()

However, this neither unloads packages nor clears the variables that were created. Since it is undocumented, it is better to avoid using this method.

restartR code

Use R Projects

Since one of the reasons to restart R is to start data analysis in a clean R session, a good approach is to use R Projects. RStudio creates a separate R session for each project.

Using the restartSession Function to Restart R in RStudio

R provides a function called restartSession that allows you to restart the R session programmatically. This can be useful when you want to include the restart process as part of a script or workflow.

# Using restartSession to restart R
rstudioapi::restartSession()

The rstudioapi::restartSession() function is part of the rstudioapi package, which is included in RStudio.

This function sends a command to RStudio to restart the R session. It’s a programmatic way to achieve the same result as using the Session menu.

restart r - rstudioapi

Using the quit Function

The quit function in R allows you to terminate the R session, and you can use it to restart R by providing appropriate arguments.

# Using quit to restart R
quit(save = "no", status = 0)

The quit function is a base R function that exits the R session. In this example, we use it with the arguments save = "no" to avoid saving the workspace and status = 0 to indicate a successful exit, effectively restarting R.

Conclusion

Restarting R in RStudio is a common task that can be accomplished through various methods. Whether you prefer using the graphical interface, keyboard shortcuts, or incorporating restarts into your scripts, RStudio provides flexibility to meet your needs.

Understanding these different methods empowers you to choose the most suitable approach based on your workflow and requirements. Incorporating these techniques into your R programming routine can enhance efficiency and help maintain a clean and reliable working environment.

References

  1. See Chapter 8 of R for Data Science to learn more about R Projects.

  2. Also, see Section 1.6 of R for Health Data Science to learn about startup options for RStudio.

Author: Jesse John
Jesse John avatar Jesse John avatar

Jesse is passionate about data analysis and visualization. He uses the R statistical programming language for all aspects of his work.