Skip to contents

cache an R object

Usage

cache(name, expr, overwrite = FALSE, time = 3600, env = parent.frame())

Arguments

name

Name of the object

expr

Expression to evaluate

overwrite

If `TRUE` saved over current objects

time

Either time (seconds) for how long to cache the object(s) or the time as a `POSIXt` object

env

Environment to load object into if found

Value

Result of `expr`, or cached result

Examples

res <- .POSIXct(0)
for (i in 1:6) {
  cache("foo", Sys.time(), time = 1)
  Sys.sleep(0.5)
  res[i] <- foo
}
res
#> [1] "2023-08-03 03:37:19 UTC" "2023-08-03 03:37:19 UTC"
#> [3] "2023-08-03 03:37:20 UTC" "2023-08-03 03:37:20 UTC"
#> [5] "2023-08-03 03:37:21 UTC" "2023-08-03 03:37:21 UTC"