geposanui/R/utils.R

32 lines
869 B
R
Raw Normal View History

2021-10-20 15:34:52 +02:00
# 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.
2021-08-25 11:59:27 +02:00
run_cached <- function(id, func, ...) {
if (!dir.exists("cache")) {
dir.create("cache")
}
cache_file <- paste("cache/", id, ".rds", sep = "")
if (file.exists(cache_file)) {
# 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
}
2021-10-19 14:15:28 +02:00
}
2021-10-20 15:34:52 +02:00
# This is needed to make data.table's and shiny's symbols available within the
# package.
#' @import data.table
#' @import shiny
NULL