01 / 21
R

RDS Files
in R Programming

Learn how to serialize and deserialize R objects using saveRDS() and readRDS() for efficient data storage and sharing.

R Programming

What Is Serialization?

Serialization is the process of converting an R object in memory into a stream of bytes that can be saved to a file. Deserialization is the reverse: reading those bytes back and reconstructing the original object.

R Object (in memory) saveRDS() .rds File (on disk) readRDS() Object

What Is an RDS File?

An RDS file (R Data Serialization) stores a single R object in a compressed binary format. It preserves the object's type, class, attributes, and all internal structure.

Single Object
Each .rds file stores exactly one R object: a vector, data frame, list, model, or any other type.
Binary Format
RDS files are compact binary, not plain text. They are typically gzip-compressed by default.
No Name Binding
Unlike .RData, the object is not tied to a variable name. You assign it freely on read.

Why Use RDS Files?

1
Preserves structure. Unlike CSV, RDS keeps factors, dates, attributes, and nested lists intact.
2
Faster I/O. Binary read/write is significantly faster than parsing text-based formats like CSV.
3
Smaller file size. Automatic gzip compression keeps .rds files compact on disk.
4
Clean variable naming. You choose the variable name when reading, avoiding environment clutter.

The Core Functions: saveRDS and readRDS

# Save a single R object to a file saveRDS(object, file = "path/to/file.rds") # Read it back into a new variable my_data <- readRDS("path/to/file.rds")

saveRDS() In Detail

saveRDS( object, # any R object file = "", # connection or file path ascii = FALSE, # use text format? compress = TRUE, # gzip compression refhook = NULL # advanced: external refs )
compress
Default is TRUE (gzip). Can also be "bzip2" or "xz" for different tradeoffs.
ascii
If TRUE, writes a text representation. Rarely used: files are larger and slower to read.

readRDS() In Detail

# readRDS returns the object — you assign it yourself result <- readRDS("my_model.rds") # You can name it anything you want best_model <- readRDS("my_model.rds") backup <- readRDS("my_model.rds")

.rds vs .RData: Key Distinction

.rds (saveRDS/readRDS) .RData (save/load)
Objects per file Exactly one One or more
Name binding No: you assign on read Yes: original names restored
Write function saveRDS(obj, file) save(obj1, obj2, file)
Read function x <- readRDS(file) load(file)
Overwrite risk Low High

.rds vs .csv: When to Choose

.rds .csv
Format Binary (compressed) Plain text
Preserves types ✓ factors, dates, lists ✕ everything becomes text
Speed Fast Slower (parsing)
Cross-platform R only Any language
Human-readable ✕ ✓

How to Apply RDS in Your Workflow

1
Create or compute your object. This could be a data frame, a fitted model, a list of results, or any R object.
2
Save with saveRDS(). Provide the object and a file path ending in .rds.
3
Read with readRDS(). Assign the result to a new variable. Verify with class() and str().
4
Use the object. It will be identical to the original: same class, same attributes, same values.

Example 1: Saving a Data Frame

# Create a data frame with different column types students <- data.frame( name = c("Alice", "Bob", "Carol"), grade = factor(c("A", "B", "A")), score = c(95.2, 87.5, 91.0) ) # Save to disk saveRDS(students, "students.rds") # Read it back loaded <- readRDS("students.rds") class(loaded$grade) # "factor" — preserved!

Example 2: Saving a Fitted Model

# Fit a linear regression model model <- lm(mpg ~ wt + hp, data = mtcars) # Save the entire model object saveRDS(model, "car_model.rds") # Later, in a different script... my_model <- readRDS("car_model.rds") # Use it for prediction — works perfectly predict(my_model, data.frame(wt = 3.0, hp = 150))

Example 3: Saving a Complex List

# A nested list with mixed types results <- list( summary = "Experiment A", values = c(1.2, 3.4, 5.6), metadata = list(date = Sys.Date(), n = 100) ) saveRDS(results, "experiment_results.rds") # Restore later exp_a <- readRDS("experiment_results.rds") exp_a$metadata$date # Date object — still a Date!

Compression Options

# Default: gzip compression (good balance) saveRDS(big_data, "data.rds") # Higher compression, slower write saveRDS(big_data, "data_xz.rds", compress = "xz") # No compression, fastest write saveRDS(big_data, "data_raw.rds", compress = FALSE)
gzip (default)
Good compression, fast speed. Best for most use cases.
xz
Smallest files, but slowest to write. Use for archiving.
FALSE
No compression. Fastest I/O, largest files. Use for speed-critical workflows.

Common Mistakes

✕ Forgetting to Assign
Writing readRDS("file.rds") without assigning the result. The object is read and immediately discarded.
✕ Using load() Syntax
Calling load("file.rds") instead of readRDS(). load() expects .RData format and will error on .rds files.
✕ Wrong File Extension
Saving as .rds but reading as .RData (or vice versa). Always match the function to the file format.

Summary: RDS Cheat Sheet

Task Code
Save one object saveRDS(obj, "file.rds")
Read one object x <- readRDS("file.rds")
Use xz compression saveRDS(obj, "f.rds", compress = "xz")
No compression saveRDS(obj, "f.rds", compress = FALSE)
Check restored type class(readRDS("file.rds"))

Question 1 of 5

Which function do you use to save a single R object to an RDS file?

Question 2 of 5

What happens if you run readRDS("data.rds") without assigning the result to a variable?

Question 3 of 5

Which data type would you lose if you saved a data frame to CSV instead of RDS?

Question 4 of 5

A key advantage of .rds over .RData is that:

Question 5 of 5

Which compression option produces the smallest file size when using saveRDS()?