Add a generic caching function

This commit is contained in:
Elias Projahn 2021-08-25 11:59:27 +02:00
parent 400ca776e0
commit abab22a907
4 changed files with 32 additions and 25 deletions

2
.gitignore vendored
View file

@ -1 +1 @@
/input/cache.rds
/cache

23
data.R
View file

@ -1,28 +1,5 @@
library(data.table)
#' Load and preprocess input data from `path`.
#'
#' A file named `cache.rds` will be created within that directory to reuse the
#' results for future runs. To forcefully recompute, delete that file.
#'
#' @seealso [load_data()]
load_data_cached <- function(path) {
cache_file <- paste(path, "cache.rds", sep = "/")
if (!file.exists(cache_file)) {
# If the cache file doesn't exist, we have to do the computation.
data <- load_data(path)
# The results are cached for the next run.
saveRDS(data, cache_file)
data
} else {
# If the cache file exists, we restore the data from it.
readRDS(cache_file)
}
}
#' Merge genome data from files in `path` into `data.table`s.
#'
#' The result will be a list with named elements:

View file

@ -4,8 +4,9 @@ library(shiny)
source("data.R")
source("scatter_plot.R")
source("util.R")
data <- load_data_cached("input")
data <- run_cached("input", load_data, "input")
server <- function(input, output) {
output$genes <- renderDT({

29
util.R Normal file
View file

@ -0,0 +1,29 @@
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
}
}