HOWTO · R
How to Restart R in RStudio
Learn how to restart an R session in RStudio from the interface or with the rstudioapi package, and what a restart resets.
On this page
To restart an R session in RStudio, choose Session > Restart R or press Ctrl + Shift + F10 (on Mac, use Command in place of Ctrl). A restart clears objects from the current session and starts R again; it does not guarantee that the operating system immediately returns all memory to other applications.
Why Restart an R Session in RStudio
RStudio runs R code in a session. When you work across several scripts in the same session, they can share objects in the Global Environment. For example, assigning a new value to an object name changes what later code using that name sees, even if that code is in another script.
Restarting gives you a fresh R process and removes the objects held in that session. Packages attached to the old session are no longer attached after the restart, so load the packages and create the objects your analysis needs. A restart can help you check that a script works from a clean start. Memory reclamation depends on R and the operating system; do not rely on a restart as a guarantee that a particular amount of memory will become available.
Restart R from the RStudio Interface
For a quick restart, use the RStudio interface:
- Select Session > Restart R.
- Or press Ctrl + Shift + F10. On Mac, press Command + Shift + F10.
The menu route is useful if the shortcut differs on your platform or in your RStudio version. Save any work you need before restarting. After R starts again, rerun the required setup code, such as loading packages and reading data.
Restart R Programmatically: Supported and Undocumented Options
When code needs to request a restart from an active RStudio session, use rstudioapi::restartSession() from the rstudioapi package. The package must be available to the R session, and the call must run in RStudio. This sends the restart request to the IDE; it is not a general-purpose way to restart an R process launched outside RStudio. This call requests a restart from the active RStudio session.
rstudioapi::restartSession()
The internal command .rs.restartR() is undocumented and tied to RStudio internals. Avoid relying on it in scripts or workflows: its behavior is not a supported public interface and may change. It is not a recommended alternative to the documented menu or the rstudioapi API.
Calling R’s quit() function, for example quit(save = "no", status = 0), terminates the R process. It does not itself start a replacement process, so it is not a restart method.
Use RStudio Projects for Separate Sessions
An RStudio Project keeps related files and project settings together, including the project’s working directory. Projects can make it easier to keep analysis work organized, and RStudio can associate workspace state with a project according to its settings. A project is not, by itself, a guarantee that multiple R processes run simultaneously; use a separate RStudio window or another process when you need concurrent sessions.
References
- Chapter 8 of R for Data Science explains the use of projects in an R workflow.
- Section 1.6 of R for Health Data Science discusses restarting R regularly and RStudio startup options.