save_run: Cache result

Description Usage Arguments Value Examples

View source: R/save-run.R

Description

Cache the result of code in an RDS file.

Usage

1
save_run(code, file, timing = TRUE)

Arguments

code

Code to run. Do not forget to wrap it with { }. Also, beware that it is your job to make sure your code and data has not changed. If this is the case, you need to remove the file storing the outdated result.

file

File path where the result is stored. Should have extension rds.

timing

Whether to print timing of running code? Default is TRUE.

Value

The evaluation of code the first time, the content of file otherwise.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# Prepare some temporary file
tmp <- tempfile(fileext = ".rds")

# Run once because result does not exist yet
save_run({
  Sys.sleep(2)
  1
}, file = tmp)

# Skip run because the result already exists
# (but still output how long it took the first time)
save_run({
  Sys.sleep(2)
  1
}, file = tmp)

runonce documentation built on Oct. 2, 2021, 9:06 a.m.