mirror of
https://github.com/johrpan/geposanui.git
synced 2025-10-26 11:17:24 +01:00
Convert to R package
This commit is contained in:
parent
b2e2dbf1af
commit
d4611f47cf
15 changed files with 799 additions and 114 deletions
31
R/utils.R
Normal file
31
R/utils.R
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
# 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)) {
|
||||
# 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
|
||||
}
|
||||
}
|
||||
|
||||
# This is needed to make data.table's and shiny's symbols available within the
|
||||
# package.
|
||||
#' @import data.table
|
||||
#' @import shiny
|
||||
NULL
|
||||
Loading…
Add table
Add a link
Reference in a new issue