SaveSeuratRds {SeuratObject}R Documentation

Save and Load Seurat Objects from Rds files

Description

Save and Load Seurat Objects from Rds files

Usage

SaveSeuratRds(
  object,
  file = NULL,
  move = TRUE,
  destdir = deprecated(),
  relative = FALSE,
  ...
)

LoadSeuratRds(file, ...)

Arguments

object

A Seurat object

file

Path to save object to; defaults to file.path(getwd(), paste0(Project(object), ".Rds"))

move

Move on-disk layers into dirname(file)

destdir

[Deprecated]

relative

Save relative paths instead of absolute ones

...

Arguments passed on to base::saveRDS, base::readRDS

ascii

a logical. If TRUE or NA, an ASCII representation is written; otherwise (default), a binary one is used. See the comments in the help for save.

version

the workspace format version to use. NULL specifies the current default version (3). The only other supported value is 2, the default from R 1.4.0 to R 3.5.0.

compress

a logical specifying whether saving to a named file is to use "gzip" compression, or one of "gzip", "bzip2" or "xz" to indicate the type of compression to be used. Ignored if file is a connection.

refhook

a hook function for handling reference objects.

Value

Invisibly returns file

Progress Updates with progressr

This function uses progressr to render status updates and progress bars. To enable progress updates, wrap the function call in with_progress or run handlers(global = TRUE) before running this function. For more details about progressr, please read vignette("progressr-intro")

Note

This function requires the fs package to be installed

See Also

saveRDS() readRDS()

Examples

if (requireNamespace("fs", quietly = TRUE)) {
  # Write out with DelayedArray
  if (requireNamespace("HDF5Array", quietly = TRUE)) {
    pbmc <- pbmc_small

    pbmc[["disk"]] <- CreateAssay5Object(list(
      mem = LayerData(pbmc, "counts"),
      disk = as(LayerData(pbmc, "counts"), "HDF5Array")
    ))

    # Save `pbmc` to an Rds file
    out <- tempfile(fileext = ".Rds")
    SaveSeuratRds(pbmc, file = out)

    # Object cache
    obj <- readRDS(out)
    Tool(obj, "SaveSeuratRds")

    # Load the saved object with on-disk layers back into memory
    pbmc2 <- LoadSeuratRds(out)
    pbmc2
    pbmc2[["disk"]]
  }

  # Write out with BPCells
  if (requireNamespace("BPCells", quietly = TRUE)) {
    pbmc <- pbmc_small

    bpm <- BPCells::write_matrix_dir(LayerData(pbmc, "counts"), dir = tempfile())
    bph <- BPCells::write_matrix_hdf5(
      LayerData(pbmc, "counts"),
      path = tempfile(fileext = ".h5"),
      group = "counts"
    )
    pbmc[["disk"]] <- CreateAssay5Object(list(dir = bpm, h5 = bph))

    # Save `pbmc` to an Rds file
    out <- tempfile(fileext = ".Rds")
    SaveSeuratRds(pbmc, file = out)

    # Object cache
    obj <- readRDS(out)
    Tool(obj, "SaveSeuratRds")

    # Load the saved object with on-disk layers back into memory
    pbmc2 <- LoadSeuratRds(out)
    pbmc2
    pbmc2[["disk"]]
  }
}


[Package SeuratObject version 5.0.2 Index]