onetime_only {onetime} | R Documentation |
Wrap a function to be called only once
Description
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 |
path |
Directory to store lockfiles. The default uses a unique
directory corresponding to the calling package, beneath
|
default |
Value to return from |
without_permission |
Character string. What to do if the user hasn't
given permission to store files? |
Value
A wrapped function. The function itself returns the result of .f
,
or default
if the inner function was not called.
See Also
Examples
oo <- options(onetime.dir = tempdir(check = TRUE))
id <- sample(10000L, 1)
sample_once <- onetime_only(sample, id = id)
sample_once(1:10)
sample_once(1:10)
onetime_reset(id)
options(oo)