Skip to contents

Assigns a new object by name to the current working environment then saves the newly assigned object, specified by the "dir" argument.

Usage

assignAndSaveData(
  name,
  object,
  dir = getOption(x = "acid.save.dir", default = getwd()),
  ext = getOption(x = "acid.save.ext", default = "rds"),
  overwrite = getOption(x = "acid.overwrite", default = TRUE),
  compress = getOption(x = "acid.save.compress", default = TRUE),
  envir = parent.frame()
)

Arguments

name

character(1). Desired variable name.

object

Object.

dir

character(1). Directory path.

ext

character(1). Output file format extension.

Supported arguments:

  • "rds": R data serialized (RDS).

  • "rda": R data (RDA).

RDS is preferred when saving single objects per file, which is always the convention of saveData(), regardless of the extension used.

overwrite

logical(1). Overwrite existing file on disk.

compress

logical(1) or character(1). These character strings are currently allowed for save(): "gzip", "bzip2", or "xz".

envir

environment. Environment to use for assignment. Defaults to parent.frame(), the calling environment.

Value

Invisible named character(1). File path.

Note

This function attempts to follow the same order as assign().

Updated 2022-06-02.

Examples

x <- 1L
dir <- AcidBase::tempdir2()
assignAndSaveData(
    name = "example",
    object = x,
    dir = dir,
    ext = "rds"
)
#> → Saving example.rds to /private/var/folders/l1/8y8sjzmn15v49jgrqglghcfr0000gn/T/RtmpX02vhQ/ZYiChV7MTc-170267960652544.
exists("example", inherits = FALSE)
#> [1] TRUE
file.exists(file.path(dir, "example.rds"))
#> [1] TRUE
AcidBase::unlink2(dir)