Run Code Only Once

Allows code to be run only once on a given computer, using lockfiles. Typical use cases include startup messages shown only when a package is loaded for the very first time.


CRANstatus R-universe Lifecycle:experimental R-CMD-check Codecov testcoverage

onetime

Listen very carefully. I shall say this only once.

Michelle Dubois, ’Allo ’Allo

The onetime package provides convenience functions to run R code only once (ever) per user. For example, you can:

  • Show a startup message only the first time (ever) that a package is loaded.
  • Run cleanup code just once after an upgrade.
  • Show the user a message, with the option not to show it again.

Onetime is a lightweight package. It requires just two package dependencies, rappdirs and filelock, with no further indirect dependencies. The total size including these dependencies is less than 50 Kb.

Example

library(onetime)

ids  <- paste0("onetime-readme-", 1:3) 

for (i in 1:5) {
  onetime_do(cat("This command will only be run once.\n"), id = ids[1])
  onetime_warning("This warning will only be shown once.", id = ids[2])
  onetime_message("This message will only be shown once.", id = ids[3])
}
#> This command will only be run once.
#> Warning: This warning will only be shown once.
#> This message will only be shown once.

Installation

Install onetime from r-universe:

install.packages("onetime", repos = c("https://hughjonesd.r-universe.dev", 
                                      "https://cloud.r-project.org"))

Or on CRAN:

install.packages("onetime")

Or install the development version from github with:

# install.packages("remotes")
remotes::install_github("hughjonesd/onetime")

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("onetime")

0.2.0 by David Hugh-Jones, 3 years ago


https://github.com/hughjonesd/onetime, https://hughjonesd.github.io/onetime/


Report a bug at https://github.com/hughjonesd/onetime/issues


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


Authors: David Hugh-Jones [aut, cre]


Documentation:   PDF Manual  


MIT + file LICENSE license


Imports rappdirs, filelock

Suggests callr, covr, devtools, doctest, knitr, lifecycle, mockr, rlang, rmarkdown, testthat, withr


See at CRAN