save_objects: Save objects to specified directory

Description Usage Arguments Details Value Examples

View source: R/save_objects.R

Description

Saves objects specified using dots and/or a list. Ensures that all names are unique (appending with "-uni_<ind>" for duplicated names). Ensures that all objects have names (appending with "-obj_<ind>") for objects without names.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
save_objects(
  ...,
  obj_list = NULL,
  dir_proj,
  dir_sub = NULL,
  empty = TRUE,
  silent = FALSE,
  gg_device = "png",
  height = 10,
  width = 12
)

Arguments

...

Objects to be saved. Preferrably specify name-value pairings, otherwise default values will be given. Objects are saved with the name appended by ".rds".

obj_list

list. List where the names (if supplied) are the names to save the corresponding values in the list as. If names are not supplied, then they will be created. Note that if you want to save ggplot2 plots as "PNG" or "PDF" images with custom names, then you need to pass them to the function as a named list to the obj_list parameter, e.g. obj_list = list("Exploratory plot" = p_exp) will save the plot p_exp with name "Exploratory plot.png" (or .pdf, if that's chosen below.)

dir_proj

character.

dir_sub

character vector or character list. Each element appends a new directory to dir_proj, in the order they're given. If not provided, then dir_proj is the final directory.

empty

logical. If TRUE, then the directory to be saved to is attempted to be deleted before saving any new files to it. This attempt fails on Windows 10, at least, if any files from these directories are open, or even if just a Windows Explorer object is open to that directory or a a sub-directory. Default is TRUE.

silent

logical. If FALSE, then warnings and messages are suppressed. Default is TRUE.

gg_device

'rds', 'png' or 'pdf. Specifies device to save objects of class 'gg'. They are saved using cowplot::ggsave2 function. If gg_device does not contain any of rds, png or pdf, then saved as rds. Default is 'png'.

width, height

numeric. Height and width to pass to cowplot::ggsave2. Default is 12 and 10.

Details

Files that already exist before they are saved are deleted. This is to ensure that on Windows the "Date Modified" column in Windows Explorer updates.

Value

invisible{TRUE}.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# standard use
dir_proj <- tempdir()
.save_objects(x = x, y = y, dir_proj = dir_proj, dir_sub = "test")
for(obj in c('x', 'y')) print(file.exists(file.path(dir_proj, "test",
                                                    paste0(obj, ".rds"))))
readRDS(file.path(dir_proj, "test", "x.rds"))
readRDS(file.path(dir_proj, "test", "y.rds"))

# ensure all objects have names
.save_objects('a', obj_list = list('c', 100, z = 4), dir_proj = dir_proj, dir_sub = "test") # warning printed
readRDS(file.path(dir_proj, "test", "obj_1.rds"))
readRDS(file.path(dir_proj, "test", "obj_2.rds"))
readRDS(file.path(dir_proj, "test", "obj_3.rds"))
readRDS(file.path(dir_proj, "test", "z.rds")) # objects with names given do not have names overwritten

# ensure that objects have unique names
.save_objects(x = 'a', x = 'b', dir_proj = dir_proj, dir_sub = "test") # warning printed
readRDS(file.path(dir_proj, "test", "x-uni_1.rds"))
readRDS(file.path(dir_proj, "test", "x-uni_2.rds"))

MiguelRodo/gamlsspipeline documentation built on July 25, 2020, 7:23 p.m.