Amazon holiday deals See deals →ad

Mets Minor League Blog

Music theory, instruments and musicians

Mets Minor League Blog

Music theory, instruments and musicians

Restart R Session Command: How to Do It

Restarting your R session is the digital equivalent of taking a deep breath and starting over — it clears your workspace, unloads packages, and resets options that may be causing weird behavior. This recipe gives you the exact commands and keyboard shortcuts to restart R from RStudio, the R console, or even programmatically, so you can get back to clean, reproducible work fast.

Ingredients

  • 1 running R session (RStudio or plain R console)
  • 1 keyboard (Ctrl+Shift+F10 on Windows/Linux, Cmd+Shift+F10 on Mac)
  • 1 mouse (optional, for menu navigation)
  • 1 R script or command line (if you prefer a programmatic restart)
  • 1 saved .RData file (if you want to preserve your workspace)

Instructions

  1. Save your work. Before restarting, save any unsaved scripts (Ctrl+S / Cmd+S) and note any objects you need. The restart will clear your workspace unless you choose to save it.
  2. Use the RStudio shortcut. Press Ctrl+Shift+F10 (Windows/Linux) or Cmd+Shift+F10 (Mac) to restart the R session immediately. This is the fastest method.
  3. Alternatively, use the menu. In RStudio, click Session → Restart R (or Restart R and Clear Output if you want to clear the console too).
  4. For plain R console (no RStudio): Type q() and press Enter. When prompted to save the workspace, choose “n” (no) to discard or “y” (yes) to save. Then reopen R.
  5. Programmatic restart (advanced): If you are running R from a script and want to restart within the session, you can use system("Rscript -e 'q()'") but this is rarely useful. A more common approach is to use .rs.restartR() in RStudio (type it in the console and press Enter).
  6. After restart, re-run your setup. Once the session restarts, you’ll see a fresh console. Re-load any packages with library() and re-source your scripts as needed.

Tips

  • Keyboard shortcut is king: Memorize Ctrl+Shift+F10 (or Cmd+Shift+F10). It’s the fastest way to restart, and you don’t have to navigate menus.
  • Clear output too: If you want to remove all console output as well, use Session → Restart R and Clear Output (or press Ctrl+Shift+F10 twice? No—just use the menu option). This is great for presentations or when you want a pristine console.
  • Don’t save .RData unless you must: Saving the workspace on exit can cause confusion later because objects reappear unexpectedly. It’s better to save specific objects with saveRDS() or save() and load them explicitly.
  • Common mistake: Forgetting that restarting clears all loaded packages and options. If you have a long setup script, consider creating an init.R file that loads all packages and sets options, so you can source it after every restart.

Serving Suggestions

Restart your R session whenever you encounter persistent errors, memory issues, or after installing a package that requires a fresh session. It’s also a good habit before starting a new analysis to ensure a clean slate. Pair the restart with a quick sessionInfo() check to verify your environment is correct.

Variations

  • Restart and clear output: In RStudio, use Session → Restart R and Clear Output to also wipe the console history.
  • Restart from a script: If you are running a long script and want to restart at a certain point, you can use source("your_script.R") after a manual restart, but there is no built-in command to restart from within a script (except in RStudio with .rs.restartR()).
  • Use a project-based restart: In RStudio, you can set up a project and restart the session with the project’s .Rprofile automatically loading your preferred packages.

Storage and Reheating

Restarting is not something you store — it’s immediate. However, if you want to “reheat” your session with the same state, you can save your workspace to an .RData file (using save.image()) before restarting, and then load it after restart with load(".RData"). Be aware that this can take a few seconds to a minute depending on the size of your objects. For reproducibility, it’s better to save your code and data separately, not the whole workspace.

Frequently Asked Questions

Will restarting R lose my unsaved script? No, unsaved scripts in RStudio are not lost — they remain open in the editor. However, any unsaved changes to the script will be lost if you close the file without saving. The restart only clears the R environment (objects, functions, etc.), not your script files.

What is the difference between restarting and clearing the console? Clearing the console (Ctrl+L) just removes the text output from the console pane. It does not affect your workspace or loaded packages. Restarting R actually terminates the R process and starts a new one, which clears all objects and unloads all packages.

Can I restart R without RStudio? Yes, in the plain R console you can type q() and then start R again. Alternatively, you can use system("R") to launch a new R process, but that’s messy. The simplest is to quit and reopen.

Does restarting R help with memory issues? Yes, if your session has accumulated large objects or memory leaks, restarting frees up all that memory. It’s a common first step when R becomes sluggish or runs out of memory.

Why does R ask to save my workspace when I quit? That prompt appears because R can save all objects to a file named .RData in the working directory. If you choose “yes”, it will save the workspace, and next time you start R in that directory, it will load those objects automatically. If you don’t want that, choose “no”.

Is there a keyboard shortcut for restarting in plain R? No, the plain R console does not have a built-in restart shortcut. You have to quit and reopen. Only RStudio provides the convenient Ctrl+Shift+F10 shortcut.

Restart R Session Command: How to Do It
Scroll to top