Large R objects are common in single-cell RNA sequencing, spatial transcriptomics and other high-throughput analyses. Examples include:
The standard saveRDS() function can compress an object
using gzip, bzip2 or xz. However,
compression performed internally by base R is not configured for
multithreaded execution.
On Linux, external utilities such as XZ and
pigz can be connected to R through a pipe. R serializes
the object and streams the serialized data to the external compressor.
The reverse process decompresses the stream and passes it to
readRDS().
This approach avoids creating a separate, uncompressed intermediate RDS file.
| Method | Compression characteristics | Multithreaded through saveRDS() |
|---|---|---|
FALSE |
No compression | Not applicable |
"gzip" |
Good general-purpose compression | No |
"bzip2" |
Often slower; compression varies by data | No |
"xz" |
Usually offers a high compression ratio | No |
| Utility | Format | Typical use |
|---|---|---|
xz |
XZ | Compact storage |
pigz |
GZIP | Fast, parallel GZIP compression |
pixz |
XZ | Parallel XZ compression with block-based processing |
The best option depends on the object, CPU, available memory, storage and whether compact files or fast access are more important.
On Ubuntu or Debian:
Check that the utilities are available:
XZ is useful when reducing storage requirements is a priority. The example below uses eight compression threads and compression level 3.
# Object to save: obj
output_file <- "large_object.rds.xz"
# Compression settings
threads <- 8
compression_level <- 3
# Create a connection to the external XZ compressor
cmd <- paste(
"xz",
paste0("-T", threads),
paste0("-", compression_level),
"-c >",
shQuote(output_file)
)
con <- pipe(cmd, open = "wb")
tryCatch(
{
saveRDS(
obj,
file = con,
compress = FALSE
)
},
finally = {
close(con)
}
)-T8: Allows XZ to use up to eight threads.-3: Sets the XZ compression level to 3.-c: Writes compressed output to standard output.>: Redirects the compressed stream to the output
file.compress = FALSE is essential here. It tells R to
serialize the object without applying a second compression layer. The
external XZ process performs the compression.
input_file <- "large_object.rds.xz"
threads <- 8
cmd <- paste(
"xz",
paste0("-T", threads),
"-d -c",
shQuote(input_file)
)
con <- pipe(cmd, open = "rb")
tryCatch(
{
obj <- readRDS(con)
},
finally = {
close(con)
}
)The decompressed R serialization stream is passed directly to
readRDS().
Note: Multithreaded XZ decompression depends on how the compressed file is structured. Streaming an R object through
xzdoes not guarantee that decompression will scale across all requested threads. R’s serialization and deserialization steps are also not made multithreaded by this method.
pigz is a parallel implementation of GZIP. It is often a
practical choice when compression and decompression speed are
important.
The following functions make it easier to save and load different R objects without repeating the pipe code.
Always verify a saved file before removing or replacing the original object.
# Load the saved object
test_obj <- read_rds_xz(
"seurat_object.rds.xz",
threads = 8
)
# Inspect the object
class(test_obj)
dim(test_obj)
# For Seurat objects
inherits(test_obj, "Seurat")
# Release the verification object
rm(test_obj)
gc()For other object types, use appropriate checks. For example, inspect the dimensions of a matrix or the structure of a list.
The number of compression threads should be selected according to the available CPU resources and memory.
For example:
or:
More threads do not always result in proportionally better performance. Compression can become limited by memory bandwidth, storage throughput or the characteristics of the data.
It is also important to leave sufficient CPU and memory resources for other running analyses.
Compression does not remove the memory required to hold an R object in memory. The object must be available to R for serialization.
Streaming compression avoids creating a separate uncompressed intermediate file. However, the destination filesystem still needs enough free space for the compressed output.
Use extensions that indicate the external compression format:
| Extension | Format |
|---|---|
.rds |
Uncompressed RDS |
.rds.gz |
GZIP-compressed RDS |
.rds.xz |
XZ-compressed RDS |
The extension is a naming convention. The actual compression format is determined by the external utility.
The examples use R pipe connections to stream data to or from external commands. For important or irreplaceable datasets, check that the output file exists, has a plausible size and can be read successfully before deleting any source data.
A successful saveRDS() call alone should not be treated
as a complete backup-verification procedure.
RDS is convenient for saving complete R objects, including metadata, dimensional reductions, graphs and analysis results. However, extremely large expression matrices may benefit from disk-backed storage.
| Technology | Typical application |
|---|---|
| HDF5 | Large numerical datasets and expression matrices |
| HDF5Array | Delayed, disk-backed matrix operations |
| BPCells | Efficient on-disk single-cell expression data |
| h5Seurat | HDF5-based storage for Seurat objects |
| Loom | Single-cell data storage and exchange |
For datasets that are too large to comfortably fit in RAM, consider an appropriate disk-backed format and workflow rather than relying only on compression.
| Requirement | Suggested approach |
|---|---|
| Compact storage | XZ compression |
| Parallel GZIP compression | pigz |
| Convenient complete-object storage | RDS |
| Very large disk-backed expression matrices | HDF5 or BPCells |
| Reusable R workflows | Custom save/load functions |
Practical recommendation: Use pigz for
parallel GZIP compression when convenient access is important, and XZ
when compact storage is the priority. Test both approaches with
representative objects from your own workflow.
Remember that external multithreaded compression accelerates the compression or decompression stage; it does not make R’s serialization or deserialization itself multithreaded.