save_run {runonce} | R Documentation |
Cache the result of code
in an RDS file.
save_run(code, file, timing = TRUE)
code |
Code to run. Do not forget to wrap it with |
file |
File path where the result is stored. Should have extension |
timing |
Whether to print timing of running code? Default is |
The evaluation of code
the first time, the content of file
otherwise.
# 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)