1 Overview

Large R objects are common in single-cell RNA sequencing, spatial transcriptomics and other high-throughput analyses. Examples include:

  • Seurat and SingleCellExperiment objects
  • Visium and Visium HD spatial transcriptomics objects
  • Large gene-expression matrices
  • Integrated multi-sample datasets
  • Normalized expression matrices and dimensionality-reduction results
  • Machine-learning models and intermediate analysis objects

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.

2 1. Compression options

2.1 Built-in R compression

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

2.2 External parallel compressors

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.

3 2. Install the Linux utilities

On Ubuntu or Debian:

sudo apt update
sudo apt install xz-utils pigz

Check that the utilities are available:

xz --version
pigz --version

4 3. Parallel XZ compression

XZ is useful when reducing storage requirements is a priority. The example below uses eight compression threads and compression level 3.

4.1 Save an R object

# 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)
  }
)

4.1.1 Understanding the command

xz -T8 -3 -c
  • -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.

4.2 Load an XZ-compressed R object

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 xz does not guarantee that decompression will scale across all requested threads. R’s serialization and deserialization steps are also not made multithreaded by this method.

5 4. Parallel GZIP compression with pigz

pigz is a parallel implementation of GZIP. It is often a practical choice when compression and decompression speed are important.

5.1 Save an R object

output_file <- "large_object.rds.gz"
threads <- 8

cmd <- paste(
  "pigz",
  paste0("-p", threads),
  "-6 -c >",
  shQuote(output_file)
)

con <- pipe(cmd, open = "wb")

tryCatch(
  {
    saveRDS(
      obj,
      file = con,
      compress = FALSE
    )
  },
  finally = {
    close(con)
  }
)

5.1.1 Understanding the command

pigz -p 8 -6 -c
  • -p 8: Uses eight compression threads.
  • -6: Sets the GZIP compression level to 6.
  • -c: Writes compressed output to standard output.

5.2 Load a GZIP-compressed R object

input_file <- "large_object.rds.gz"
threads <- 8

cmd <- paste(
  "pigz",
  paste0("-p", threads),
  "-d -c",
  shQuote(input_file)
)

con <- pipe(cmd, open = "rb")

tryCatch(
  {
    obj <- readRDS(con)
  },
  finally = {
    close(con)
  }
)

6 5. Reusable functions

The following functions make it easier to save and load different R objects without repeating the pipe code.

6.1 Save using XZ

save_rds_xz <- function(object, file, threads = 8, level = 3) {

  cmd <- paste(
    "xz",
    paste0("-T", threads),
    paste0("-", level),
    "-c >",
    shQuote(file)
  )

  con <- pipe(cmd, open = "wb")

  tryCatch(
    {
      saveRDS(object, file = con, compress = FALSE)
    },
    finally = {
      close(con)
    }
  )

  invisible(file)
}

6.2 Load using XZ

read_rds_xz <- function(file, threads = 8) {

  cmd <- paste(
    "xz",
    paste0("-T", threads),
    "-d -c",
    shQuote(file)
  )

  con <- pipe(cmd, open = "rb")

  tryCatch(
    {
      readRDS(con)
    },
    finally = {
      close(con)
    }
  )
}

6.3 Save using pigz

save_rds_gz <- function(object, file, threads = 8, level = 6) {

  cmd <- paste(
    "pigz",
    paste0("-p", threads),
    paste0("-", level),
    "-c >",
    shQuote(file)
  )

  con <- pipe(cmd, open = "wb")

  tryCatch(
    {
      saveRDS(object, file = con, compress = FALSE)
    },
    finally = {
      close(con)
    }
  )

  invisible(file)
}

6.4 Load using pigz

read_rds_gz <- function(file, threads = 8) {

  cmd <- paste(
    "pigz",
    paste0("-p", threads),
    "-d -c",
    shQuote(file)
  )

  con <- pipe(cmd, open = "rb")

  tryCatch(
    {
      readRDS(con)
    },
    finally = {
      close(con)
    }
  )
}

6.5 Example usage

# Save
save_rds_xz(
  object = obj,
  file = "seurat_object.rds.xz",
  threads = 8,
  level = 3
)

# Load
obj <- read_rds_xz(
  file = "seurat_object.rds.xz",
  threads = 8
)

For GZIP:

# Save
save_rds_gz(
  object = obj,
  file = "seurat_object.rds.gz",
  threads = 8,
  level = 6
)

# Load
obj <- read_rds_gz(
  file = "seurat_object.rds.gz",
  threads = 8
)

7 6. Verify the saved object

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.

8 7. Choosing the number of threads

The number of compression threads should be selected according to the available CPU resources and memory.

For example:

threads <- 8

or:

threads <- 16

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.

9 8. Important considerations

9.1 Memory

Compression does not remove the memory required to hold an R object in memory. The object must be available to R for serialization.

9.2 Disk space

Streaming compression avoids creating a separate uncompressed intermediate file. However, the destination filesystem still needs enough free space for the compressed output.

9.3 File extensions

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.

9.4 Pipe and process errors

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.

10 9. When RDS is not the best option

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.

11 10. Summary

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.