Skip to contents

Saves all objects (except functions and specified exclusions) from the global environment as a named list in an .RData file. Returns a summary of the saved objects' sizes in memory.

Usage

save_session(out_directory = getwd(), exclude_objects = NULL, prefix = "S")

Arguments

out_directory

Character. Directory path where the .RData file is saved. Defaults to the current working directory base::getwd().

exclude_objects

Character vector. Names of objects to exclude from saving. Defaults to NULL.

prefix

Character. Prefix for the saved file name. Defaults to "S".

Value

A tibble with columns object (object names) and size (size in MB, rounded to 1 decimal place) for the saved objects, sorted by size in descending order.

Examples

load_packages(fs, purrr)

# Create sample objects in the global environment
assign("df", data.frame(a = 1:1000), envir = .GlobalEnv)
assign("vec", rnorm(1000), envir = .GlobalEnv)
assign("fun", function(x) x + 1, envir = .GlobalEnv)
ls(.GlobalEnv)
#>  [1] "AA1"             "AA2"             "R"               "chelsa_var_info"
#>  [5] "df"              "envir"           "fun"             "ifnotfound"     
#>  [9] "inherits"        "mode"            "nm"              "object"         
#> [13] "vec"             "x"               "y"              

# Save objects to a unique temporary directory, excluding "vec"
temp_dir <- fs::path_temp("save_session")
fs::dir_create(temp_dir)

(result <- save_session(out_directory = temp_dir, exclude_objects = "vec"))
#> Saved objects to:
#> /tmp/RtmphTiYIO/save_session/S_20250610_1450.RData
#> # A tibble: 13 × 2
#>    object           size
#>    <chr>           <dbl>
#>  1 AA1              38.1
#>  2 AA2               0.4
#>  3 R                 0  
#>  4 chelsa_var_info   0  
#>  5 df                0  
#>  6 envir             0  
#>  7 ifnotfound        0  
#>  8 inherits          0  
#>  9 mode              0  
#> 10 nm                0  
#> 11 object            0  
#> 12 x                 0  
#> 13 y                 0  

# Load saved objects
saved_files <- list.files(
  temp_dir, pattern = "S_.+\\.RData$", full.names = TRUE)
if (length(saved_files) == 0) {
  stop("No RData files found in temp_dir")
}
# pick the most recent file, if there is more than one file
(saved_file <- saved_files[length(saved_files)])
#> [1] "/tmp/RtmphTiYIO/save_session/S_20250610_1450.RData"

saved_objects <- ecokit::load_as(saved_file)
names(saved_objects)
#>  [1] "AA1"             "AA2"             "R"               "chelsa_var_info"
#>  [5] "df"              "envir"           "ifnotfound"      "inherits"       
#>  [9] "mode"            "nm"              "object"          "x"              
#> [13] "y"              
str(saved_objects, 1)
#> List of 13
#>  $ AA1            : int [1:10000000] 1 2 3 4 5 6 7 8 9 10 ...
#>  $ AA2            : int [1:100000] 1 2 3 4 5 6 7 8 9 10 ...
#>  $ R              :Formal class 'PackedSpatRaster' [package "terra"] with 3 slots
#>  $ chelsa_var_info: tibble [46 × 6] (S3: tbl_df/tbl/data.frame)
#>  $ df             :'data.frame':	1000 obs. of  1 variable:
#>  $ envir          :<environment: 0x556b9cfbe8c8> 
#>  $ ifnotfound     : NULL
#>  $ inherits       : logi TRUE
#>  $ mode           : chr "any"
#>  $ nm             : NULL
#>  $ object         : NULL
#>  $ x              : NULL
#>  $ y              : logi TRUE

setdiff(
  ls(.GlobalEnv),
  c(result$object, "saved_file", "result", "saved_objects", "temp_dir")) %>%
  purrr::map(~ stats::setNames(class(get(.x, envir = .GlobalEnv)), .x)) %>%
  unlist()
#>        fun        vec 
#> "function"  "numeric" 

# Clean up
fs::dir_delete(temp_dir)