The value for each option can be set as a list which may contain more configurations on the option:
opt = set_opt(
"a" = list(.value = 1,
.length = 1,
.class = "numeric",
.validate = function(x) x > 0,
.failed_msg = "'a' should be a positive number.",
.filter = function(x) ifelse(x > 10, 10, x),
.read.only = FALSE,
.visible = TRUE,
.private = FALSE),
"b" = "text"
)
In above example, option value for a should pass following conditions:
length should be 1;
should have numeric class;
should be positive;
if the value is larger than 10, it will be enforced to 10.
Other fields mean:
default value of a is 1;
it is not read-only;
it is visible;
it is public.
Dynamic option values
If the value of the option is set as a function and the class of the option is non-function.
The function will be executed everytime when querying the option. In following example, the
prefix option controls the prefix of the log message.
GlobalOptions
This package aims to provide a simple way to handle global configurations. It can:
There is a vignette in the package which explains with more detail.
Usage
The most simple way is to construct an option function (e.g.
opt()) as:Then users can get or set the options by:
Users can reset their default values by:
Advanced control on options
The value for each option can be set as a list which may contain more configurations on the option:
In above example, option value for
ashould pass following conditions:numericclass;Other fields mean:
ais 1;Dynamic option values
If the value of the option is set as a function and the class of the option is non-function. The function will be executed everytime when querying the option. In following example, the
prefixoption controls the prefix of the log message.Then
opt("prefix")will return the current time:Interact between options
One option value can depend on other option values and the value of the option changes when the value of the dependent option changes.
Local options
Local options can be created by specifying
LOCALtoTRUE. The local mode will end whenLOCALis set toFALSEexplicitely or the environment changes.License
MIT @ Zuguang Gu