Skip to contents

This function updates names of existing results by re-hashing each set of parameters with potentially updated values based on adjustments made to a hash table (see ?create_hash_table) by user. It loads RDS files based on their existing hashes, compares to the corresponding entry in a hash table, generates new hashes where needed, and saves the files with the new hashes. The old files are deleted if their hashes differ from the new ones.

Usage

update_from_hash_table(
  hash_table,
  rds_folder,
  hash_includes_timestamp = FALSE,
  ignore_na = TRUE,
  alphabetical_order = TRUE,
  algo = "xxhash64"
)

Arguments

hash_table

A file path to a modified hash table generated by create_hash_table.

rds_folder

A string specifying the directory containing the RDS files associated with the hash table.

hash_includes_timestamp

Logical; if TRUE, timestamps are included in the hash generation.

ignore_na

Logical; if TRUE, NA values are ignored during hash generation.

alphabetical_order

Logical; if TRUE, parameters are sorted alphabetically before hash generation.

algo

Character string specifying the hashing algorithm to use. Default is "xxhash64". See ?digest

Value

The function does not return a value but saves updated RDS files and deletes old files as needed.

Examples

## Setup
tmp_dir <- file.path(tempdir(), "example")
dir.create(tmp_dir)

## Save objects
obj1 <- rnorm(1000)
obj2 <- data.frame(
  x = runif(100),
  y = "something",
  z = rep(c(TRUE, FALSE), 50)
)
obj3 <- list(obj1, obj2)

params1 <- list(
  distribution = "normal",
  other_params = list(param1 = TRUE, param2 = 1, param3 = NA)
)
params2 <- list(
  distribution = "uniform",
  other_params = list(param1 = FALSE, param2 = 2, param3 = "1", param4 = 4)
)
params3 <- list(
  distribution = "composite",
  other_params = list(param1 = TRUE, param2 = 3, param3 = 1)
)

save_objects(tmp_dir, obj1, params1)
save_objects(tmp_dir, obj2, params2)
save_objects(tmp_dir, obj3, params3)

## Create hash table
create_hash_table(tmp_dir, save_path = file.path(tmp_dir, "hash_table.csv"))
#>   distribution other_params[[param1]] other_params[[param2]]
#> 1      uniform                  FALSE                      2
#> 2    composite                   TRUE                      3
#> 3       normal                   TRUE                      1
#>   other_params[[param3]] other_params[[param4]]
#> 1                      1                      4
#> 2                      1                   <NA>
#> 3                   <NA>                   <NA>
#>                            script_name             hash
#> 1 4aaef0ca-7936-4076-b370-27ac49c18337 181c74ea91113c0c
#> 2 4aaef0ca-7936-4076-b370-27ac49c18337 3a21c1fc17d5f68a
#> 3 4aaef0ca-7936-4076-b370-27ac49c18337 5964241557faff5a

## Read in hash table, make a change, and save
hash_table <- read.csv(file.path(tmp_dir, "hash_table.csv"))
hash_table$distribution <- "something different"
write.csv(hash_table, file.path(tmp_dir, "hash_table.csv"))

## See file names before change
list.files(tmp_dir)
#> [1] "181c74ea91113c0c.rds"            "181c74ea91113c0c_parameters.rds"
#> [3] "3a21c1fc17d5f68a.rds"            "3a21c1fc17d5f68a_parameters.rds"
#> [5] "5964241557faff5a.rds"            "5964241557faff5a_parameters.rds"
#> [7] "hash_table.csv"                 

update_from_hash_table(
  hash_table = file.path(tmp_dir, "hash_table.csv"),
  rds_folder = tmp_dir
)
#> New names:
#>  `` -> `...1`
#> 
#> Updating distribution in parameters_list: uniform -> something different
#> 
#> 
#> 
#> 
#> Renamed 181c74ea91113c0c.rds -> d5c78d84b1763bf9.rds; updated parameters saved.
#> 
#> Updating distribution in parameters_list: composite -> something different
#> 
#> 
#> 
#> Renamed 3a21c1fc17d5f68a.rds -> 170003d58b98db1b.rds; updated parameters saved.
#> 
#> Updating distribution in parameters_list: normal -> something different
#> 
#> 
#> Renamed 5964241557faff5a.rds -> 98ebac62f38a571e.rds; updated parameters saved.

## See difference to before running update_hash_table()
list.files(tmp_dir)
#> [1] "170003d58b98db1b.rds"            "170003d58b98db1b_parameters.rds"
#> [3] "98ebac62f38a571e.rds"            "98ebac62f38a571e_parameters.rds"
#> [5] "d5c78d84b1763bf9.rds"            "d5c78d84b1763bf9_parameters.rds"
#> [7] "hash_table.csv"                 

## Cleanup
unlink(tmp_dir, recursive = TRUE)