This takes a function and returns the same function wrapped by onetime_do()
.
Use it for code which should run only once, but which may be called from
multiple locations. This frees you from having to use the same id
multiple
times.
Usage
onetime_only(
.f,
id = deprecate_calling_package(),
path = default_lockfile_dir(),
default = NULL,
without_permission = "warn"
)
Arguments
- .f
A function
- id
Unique ID string. If this is unset, the name of the calling package will be used. Since onetime 0.2.0, not setting
id
is deprecated.- path
Directory to store lockfiles. The default uses a unique directory corresponding to the calling package, beneath
rappdirs::user_config_dir()
. Normally you should leave this as the default.- default
Value to return from
.f
if function was not executed.- without_permission
Character string. What to do if the user hasn't given permission to store files?
"warn"
runs the action with an extra warning;"run"
runs the action with no warning;"pass"
does nothing and returns the default;"stop"
throws an error;"ask"
asks for permission usingcheck_ok_to_store()
, and returns the default if it is not granted.
Value
A wrapped function. The function itself returns the result of .f
,
or default
if the inner function was not called.
Examples
oo <- options(onetime.dir = tempdir(check = TRUE))
id <- sample(10000L, 1)
sample_once <- onetime_only(sample, id = id)
sample_once(1:10)
#> [1] 10 6 7 2 4 3 8 5 1 9
sample_once(1:10)
#> NULL
onetime_reset(id)
options(oo)