R/RStudio may take long to open (or even crash) if it is set to re-load objects from previous sessions. Unfortunately, this is the default. And it is bad. Each session should be self contained, and you should be able to reproduce your analysis entirely from your script.

What if your analysis uses an object that takes a long time to produce? In that case you can use one script to create the time-consuming object, write that object into a file, and read it from any other scripts that use it. (If you’ll use that object from other projects you should consider storing it into its own package and loading it with library(object).)

First I’ll show you how to change your settings to never re-load objects from previous R sessions. Then I’ll show you how to save time by writing and then reading objects that take a long time to produce.

1. Settings

  • Go to Tools > Global Options… > R General. Ensure these two settings are as follows:

  • The next step will clean your environment. Before you do that you may want to save it. You can do that in RStudio from the Environment tab.

  • Now restart your R session (Shift + Control/Command + F10).

  • Your environment should now be empty.

Saving and re-loading objects that take long to produce

A great way to manage your workflow is to write a script that produces and saves an object, and another script that reads that object and uses it for analysis.

Example

  • Script 1: project/R/write_object.R
# This script creates a model that takes a long time to run
model1 <- lm(disp ~ cyl, data = mtcars)
model1
## 
## Call:
## lm(formula = disp ~ cyl, data = mtcars)
## 
## Coefficients:
## (Intercept)          cyl  
##      -156.6         62.6

# Writing model1 to a file so I can use reuse it
readr::write_rds(model1, "models/model1.rds")
  • You can now restart my R session (or close and reopen R/RStudio).

Script 2: project/R/read_object.R

# Prove that global environment is empty
ls()
## [1] "model1"
  • Read the object you wrote before and continue your analysis.
model1 <- readr::read_rds("models/model1.rds")
model1
## 
## Call:
## lm(formula = disp ~ cyl, data = mtcars)
## 
## Coefficients:
## (Intercept)          cyl  
##      -156.6         62.6

model1$coefficients
## (Intercept)         cyl 
##  -156.60898    62.59893