0Pricing
R Academy · Lesson

R Projects and Workspace Management

Create RStudio projects and manage .RData, .Rhistory, and .Renviron.

The R Workspace: .RData and .Rhistory

By default, when you quit R it asks to save the workspace — all objects in memory — to a hidden file called .RData in the working directory. Your command history goes to .Rhistory. These files are loaded automatically next session.

This convenience is also a reproducibility trap.

# .RData: binary snapshot of all objects in the global environment
# .Rhistory: text file of every command you typed

# See what would be saved right now:
cat('Objects that would be saved:', length(ls()), '\n')
cat('Their names:', paste(ls(), collapse = ', '))

# Workspace saving is controlled by:
# Tools -> Global Options -> General -> Save workspace (NEVER recommended)
# Or at startup: R --no-save --no-restore

Why Avoid Auto-Saving .RData?

Relying on .RData means your results cannot be reproduced from scratch. Objects accumulate invisibly across sessions, old variables shadow new ones, and collaborators cannot replicate your environment. The solution: never save the workspace — always recreate it by running your scripts.

# The recommended R startup setting (in RStudio):
# Tools -> Global Options -> General
#   Restore .RData at startup: UNCHECKED
#   Save workspace on exit: NEVER

# This forces you to write scripts that are fully self-contained.
# A fresh session that runs cleanly = reproducible analysis.

# Equivalent command-line flags:
# R --no-save --no-restore

cat('Always start fresh!')

All lessons in this course

  1. Using source() to Load Scripts
  2. Comments, Style, and Readability
  3. Working Directories and File Paths
  4. R Projects and Workspace Management
← Back to R Academy