Execute Expensive Operations Only Once

Allows you to easily execute expensive compute operations only once, and save the resulting object to disk.


once

badge

An R package that makes it easy to execute expensive operations only once.

Using once() you can wrap an expensive operation so that output is saved to disk. If the file exists, it won't run again, it will just load the saved version.

Eg. instead of

file_path <- "saved_objects/saved_random_number.Rds"

if (file.exists(file_path)){

  my_number <- readRDS(file = file_path)

} else {

  my_number <-
    runif(1e8) %>% # some expensive operation
    mean()
  # or ML model training such as 
  # `my_trained_models <- train_models_function(...)`
  
  saveRDS(my_number, file = file_path)
}

it would look like

my_number <-
  runif(1e8) %>% # some expensive operation
  mean() %>%
  once(file_path = "saved_objects/saved_random_number.Rds") 

which will only execute the expensive operation the first time the code is run, and save this output to file.

Install instructions

#install.packages("devtools")
devtools::install_github("gdmcdonald/once")
library(once)

Further details

You can also force it to run again, which will cause it to overwrite the saved object on disk:

my_number <-
  runif(1e8) %>% # some expensive operation
  mean() %>%
  once(file_path = "saved_objects/saved_random_number.Rds",
       rerun = TRUE ) # rerun and overwrite the existing file

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("once")

0.4.1 by Gordon McDonald, 4 years ago


https://gdmcdonald.github.io/once/


Browse source code at https://github.com/cran/once


Authors: Gordon McDonald [aut, cre]


Documentation:   PDF Manual  


MIT + file LICENSE license


Imports magrittr


See at CRAN