Description Usage Arguments Details Value See Also Examples
View source: R/func_save-load.R
These functions wrap base::saveRDS()
and base::readRDS()
and are
augmented with utility functions to make saving and loading R (.RDS) objects
easier.
1 2 3 | save_object(x, path, filename_modifier = NULL, sep = "_")
load_object(x, path, filename_modifier = NULL, sep = "_", auto_assign = TRUE)
|
x |
The R object to save. |
path |
A directory within the current RStudio Project. |
filename_modifier |
The saved .RDS file name takes on the R object's
name by default (when |
sep |
Ignored by default. If |
auto_assign |
Only for |
Specifically, these functions assume the use of an RStudio Project. The main benefit of these functions is the consistency between the name of the R object and the .RDS file name.
Additionally, for analyses where there are lots of objects to be
saved/loaded, there are utilities to specify where these objects will be,
e.g. set_object_path
.
Either a .RDS file written to the specified location or an R object containing the contents of the .RDS file.
set_object_path, get_object_oath, unset_object_path
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | if (interactive()) {
library(fs)
library(here)
library(waldo)
# Create a directory test-folder in the current RStudio Project path
dir_create(here("test-folder"))
# Save `mtcars` object to test-folder `here::here()` is run within
# save_object, so it doesn't need to be supplied here, though it can be.
save_object(mtcars, path = "test-folder")
# Check the objects saved. The .RDS file is automatically named with the object's name
# `mtcars.RDS`
dir_ls(here('test-folder'))
# The object can be loaded easily as:
# This automatically assigns the object loaded as mtcars.
load_object(mtcars, path = "test-folder")
# If you wish to assign an object yourself,
# for example as a different name, this can be done as follows:
carsmt <- load_object(mtcars, path = "test-folder", auto_assign = FALSE)
# .RDS files can also have a modifier to distinguish between similar R objects.
mtcars <- mtcars[,1:3]
save_object(mtcars, path = "test-folder",
filename_modifier = "cols_1-3", sep = "_")
# It can be loaded back with the same arguments
load_object(mtcars, path = "test-folder",
filename_modifier = "cols_1-3", sep = "_")
# It could also be loaded back directly by name (should be character vector)
load_object("mtcars_cols_1-3", path = "test-folder")
# For analyses where lots of objects will be reused, you can set the
# global path option and not specifically include the path.
set_object_path("test-folder")
# This now works! No path necessary.
save_object(iris)
load_object(iris)
# Custom filenames also work:
save_object(iris, filename_modifier = "123")
load_object(iris, filename_modifier = "123")
# You can still specify an alternative path even with that global option set.
dir_create("another-test-folder")
# Modify iris for example
iris$message <- "I'm in a new folder"
# Save modified iris to the new path
save_object(iris, path = "another-test-folder")
load_object(iris, path = "another-test-folder")
# Let's compare these
orig <- load_object(iris, auto_assign = FALSE)
new <- load_object(iris, path = "another-test-folder", auto_assign = FALSE)
# Should not be identical!
compare(orig, new)
# Check the path
get_object_path()
# Unset the path
unset_object_path()
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.