The once() function allows you to easily execute expensive compute operations only once, and save the resulting object to disk.

once(expr, file_path = NULL, rerun = FALSE)

Arguments

expr

The expensive expression to evaluate

file_path

File path for saving the output object as an Rds file. Note that if no file name is provided it will not save!

rerun

Rerun the expression anyway and save the result? Defaults to false.

Value

the results of expr

Examples

save_file <- tempfile(fileext = ".Rds")
 # temporary file path - replace with your preferred saved file path
my_out <-
  runif(1e8) %>% # some expensive operation
  mean() %>%
  once(file_path = save_file)
  # only do it once, save output to this file.