Learn R Programming

once

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

Copy Link

Version

Install

install.packages('once')

Monthly Downloads

156

Version

0.4.1

License

MIT + file LICENSE

Maintainer

Gordon McDonald

Last Published

August 31st, 2022

Functions in once (0.4.1)

once

Execute Expensive Operations Only Once
%>%

Pipe operator