R Under development (unstable) (2025-05-01 r88184 ucrt) -- "Unsuffered Consequences" Copyright (C) 2025 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > library("R.cache") R.cache v0.17.0 successfully loaded. See ?R.cache for help. > oopts <- options("R.cache.compress") > > simulate <- function(mean, sd) { + # 1. Try to load cached data, if already generated + key <- list(mean, sd) + data <- loadCache(key) + if (!is.null(data)) { + cat("Loaded cached data\n") + return(data) + } + + # 2. If not available, generate it. + cat("Generating data from scratch...") + data <- rnorm(1000, mean=mean, sd=sd) + Sys.sleep(1) # Emulate slow algorithm + cat("ok\n") + saveCache(data, key=key, comment="simulate()") + + data + } > > > for (compress in c(FALSE, TRUE)) { + options("R.cache.compress"=compress) + + data <- simulate(2.3, 3.0) + data <- simulate(2.3, 3.5) + data <- simulate(2.3, 3.0) # Will load cached data + + # Clean up + file.remove(findCache(key=list(2.3,3.0))) + file.remove(findCache(key=list(2.3,3.5))) + } Generating data from scratch...ok Generating data from scratch...ok Loaded cached data Generating data from scratch...ok Generating data from scratch...ok Loaded cached data > > > ## Exceptions > res <- try(findCache(key = NULL)) Error in generateCache.default(key = key, ...) : Argument 'key' must be a list, an environment: NULL > stopifnot(inherits(res, "try-error")) > > ## Cleanup > options(oopts) > > proc.time() user system elapsed 0.23 0.06 4.59