Reorganize source files and generalize presets

This commit is contained in:
Elias Projahn 2021-10-16 21:46:59 +02:00
parent 8104e9bd8a
commit 68354bf808
14 changed files with 119 additions and 147 deletions

29
util.R
View file

@ -1,29 +0,0 @@
library(rlog)
#' Run a function caching the result on the file system.
#'
#' The function will be called with the appended arguments. The [`id`] argument
#' will be used to identify the cache file on the file system and in log
#' messages.
run_cached <- function(id, func, ...) {
if (!dir.exists("cache")) {
dir.create("cache")
}
cache_file <- paste("cache/", id, ".rds", sep = "")
if (file.exists(cache_file)) {
log_info(sprintf("Loading %s from cache", id))
# If the cache file exists, we restore the data from it.
readRDS(cache_file)
} else {
# If the cache file doesn't exist, we have to do the computation.
data <- func(...)
# The results are cached for the next run.
saveRDS(data, cache_file)
data
}
}