Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

do_once

Perform a task once in an R session


Description

Perform a task once in an R session, e.g., emit a message or warning. Then give users an optional hint on how not to perform this task at all.

Usage

do_once(
  task,
  option,
  hint = c("You will not see this message again in this R session.",
    "If you never want to see this message,",
    sprintf("you may set options(%s = FALSE) in your .Rprofile.", option))
)

Arguments

task

Any R code expression to be evaluated once to perform a task, e.g., warning('Danger!') or message('Today is ', Sys.Date()).

option

An R option name. This name should be as unique as possible in options(). After the task has been successfully performed, this option will be set to FALSE in the current R session, to prevent the task from being performed again the next time when do_once() is called.

hint

A character vector to provide a hint to users on how not to perform the task or see the message again in the current R session. Set hint = "" if you do not want to provide the hint.

Value

The value returned by the task, invisibly.

Examples

do_once(message("Today's date is ", Sys.Date()), "xfun.date.reminder")
# if you run it again, it will not emit the message again
do_once(message("Today's date is ", Sys.Date()), "xfun.date.reminder")

do_once({
    Sys.sleep(2)
    1 + 1
}, "xfun.task.1plus1")
do_once({
    Sys.sleep(2)
    1 + 1
}, "xfun.task.1plus1")

xfun

Miscellaneous Functions to Support Packages Maintained by 'Yihui Xie'

v0.22
MIT + file LICENSE
Authors
Yihui Xie [aut, cre, cph] (<https://orcid.org/0000-0003-0645-5666>), Wush Wu [ctb], Daijiang Li [ctb], Xianying Tan [ctb], Salim Brüggemann [ctb] (<https://orcid.org/0000-0002-5329-5987>), Christophe Dervieux [ctb]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.