try_load: Try to load a file before parsing an expression

Description Usage Arguments Examples

View source: R/try_load.R

Description

Checks whether a file exists, and if so loads it. If the file doesn't exist, it runs an expression and optionally saves it to that filename. Useful for scripts that take a long time to run and where you want to save progress along the way.

Usage

1
try_load(filename, expr, save = FALSE, ...)

Arguments

filename

Name of the file to try to load. Should be in RDS format.

expr

Expression to run if the file doesn't exist.

save

After expression is run, save the result as filename? Default is FALSE.

...

Additional arguments to pass to saveRDS if save == TRUE.

Examples

1
2
3
4
5
6
7
8
# Load setosa_glm.RDS if it exists, otherwise create and save it with this
# script
setosa_glm <- try_load("setosa_glm.RDS", save = TRUE,
  {
setosa <- iris[, 1:3]
setosa$setosa <- ifelse(iris$Species == "setosa", 1, 0)
setosa_glm <- glm(setosa ~ ., data = setosa)
  })

Prometheus77/actools documentation built on March 7, 2020, 11:01 a.m.