Skip to contents

Load R data files from a directory using symbols rather than complete file paths. Supports RDS, RDA, and RDATA file extensions.

Usage

loadData(
  ...,
  dir = getOption(x = "acid.load.dir", default = getwd()),
  envir = globalenv(),
  list = NULL,
  overwrite = getOption(x = "acid.overwrite", default = TRUE)
)

Arguments

...

Object names. Note that these arguments are interpreted as symbols using non-standard evaluation for convenience during interactive use, and must not be quoted.

dir

character(1). Directory path.

envir

environment. Environment.

list

character. A character vector containing the names of objects to be loaded.

overwrite

logical(1). Overwrite existing file on disk.

Value

Invisible character. File paths.

Details

loadData() is opinionated about the format of R data files it will accept. save() allows for the saving of multiple objects into a single R data file. This can later result in unexpected accidental replacement of an existing object in the current environment. Since an R data file internally stores the name of an object, if the file is later renamed the object name will no longer match.

To avoid any accidental replacements, loadData() will only load R data files that contain a single object, and the internal object name must match the file name exactly. Additionally, loadData() will intentionally error if an object with the same name already exists in the destination environment.

Note

This function is desired for interactive use and interprets object names using non-standard evaluation.

Updated 2021-10-12.

See also

Examples

dir <- system.file("extdata", package = "pipette")

## Interactive mode ====
## Note that this method uses non-standard evaluation.
loadData(example, dir = dir)
#> → Loading example.rds from /private/var/folders/l1/8y8sjzmn15v49jgrqglghcfr0000gn/T/RtmpAraQSl/temp_libpath4e036075b00f/pipette/extdata.

## Clean up.
rm(example, inherits = TRUE)

## List mode ====
## Note that this method uses standard evaluation.
## Use this approach inside of functions.
list <- "example"
loadData(list = list, dir = dir)
#> → Loading example.rds from /private/var/folders/l1/8y8sjzmn15v49jgrqglghcfr0000gn/T/RtmpAraQSl/temp_libpath4e036075b00f/pipette/extdata.

## Clean up.
rm(example, inherits = TRUE)