diff --git a/.gitignore b/.gitignore index d3a2b1c..b49a22a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -/input/cache.rds +/cache \ No newline at end of file diff --git a/data.R b/data.R index a38bf04..3cbeb8c 100644 --- a/data.R +++ b/data.R @@ -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: diff --git a/server.R b/server.R index 47d4917..3424b28 100644 --- a/server.R +++ b/server.R @@ -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({ diff --git a/util.R b/util.R new file mode 100644 index 0000000..e37ece2 --- /dev/null +++ b/util.R @@ -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 + } +} \ No newline at end of file