| Title: | Generate Functions to Get or Set Global Options |
|---|---|
| Description: | It provides more configurations on the option values such as validation and filtering on the values, making options invisible or private. |
| Authors: | Zuguang Gu [aut, cre] (ORCID: <https://orcid.org/0000-0002-7395-8709>) |
| Maintainer: | Zuguang Gu <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.4 |
| Built: | 2026-06-07 08:07:45 UTC |
| Source: | https://github.com/jokergoo/globaloptions |
Get other option values
.v(opt_name, name_is_character = NA) ## S3 method for class 'InternalOptionValue' x$nm.v(opt_name, name_is_character = NA) ## S3 method for class 'InternalOptionValue' x$nm
opt_name |
The option name. |
name_is_character |
Please ignore, only used internally. |
x |
should always be written as '.v'. |
nm |
The option name. |
When setting one option, the value can be dependent on other option names. The current value of other option can be accessed by '.v(nm)' or '.v$nm'.
opt = set_opt(a = 1, b = function() .v$a*2) opt$b opt(a = 2); opt$b opt(a = 4); opt$b reset_opt(opt); opt$bopt = set_opt(a = 1, b = function() .v$a*2) opt$b opt(a = 2); opt$b opt(a = 4); opt$b reset_opt(opt); opt$b
Getter and setter functions
## S3 method for class 'GlobalOptionsFun' x[nm] dump_opt(x, nm) ## S3 method for class 'GlobalOptionsFun' x[[nm]] ## S3 replacement method for class 'GlobalOptionsFun' x[[nm]] <- value ## S3 method for class 'GlobalOptionsFun' names(x) ## S3 method for class 'GlobalOptionsFun' .DollarNames(x, pattern = "") ## S3 method for class 'GlobalOptionsFun' x$nm ## S3 replacement method for class 'GlobalOptionsFun' x$nm <- value## S3 method for class 'GlobalOptionsFun' x[nm] dump_opt(x, nm) ## S3 method for class 'GlobalOptionsFun' x[[nm]] ## S3 replacement method for class 'GlobalOptionsFun' x[[nm]] <- value ## S3 method for class 'GlobalOptionsFun' names(x) ## S3 method for class 'GlobalOptionsFun' .DollarNames(x, pattern = "") ## S3 method for class 'GlobalOptionsFun' x$nm ## S3 replacement method for class 'GlobalOptionsFun' x$nm <- value
x |
The option object returned by ['set_opt()'] or ['setGlobalOptions()']. |
nm |
A single option name. |
value |
The value which is assigned to the option. |
pattern |
Ignore. |
'[' (single bracket) returns a single option object.
'dump_opt()' is identical to '['.
'[[' (double brackets) returns the value of the option.
The '.DollarNames' method makes the option object looks like a list that it allows option name completion after '$' (by double clicking the "enter/return" key).
opt = set_opt(a = 1, b = "b") opt["a"] opt["b"] dump_opt(opt, "a") dump_opt(opt, "b") opt[["a"]] opt[["b"]] opt[["a"]] = 200 opt[["a"]] names(opt) opt$a opt$a = 100 opt$aopt = set_opt(a = 1, b = "b") opt["a"] opt["b"] dump_opt(opt, "a") dump_opt(opt, "b") opt[["a"]] opt[["b"]] opt[["a"]] = 200 opt[["a"]] names(opt) opt$a opt$a = 100 opt$a
Print options
## S3 method for class 'GlobalOptionsFun' print(x, ...)## S3 method for class 'GlobalOptionsFun' print(x, ...)
x |
The option object returned by ['set_opt()'] or ['setGlobalOptions()']. |
... |
Other arguments. |
opt = set_opt(a = 1, b = "b") optopt = set_opt(a = 1, b = "b") opt
Helper functions
reset_opt(opt) add_opt(opt, ...)reset_opt(opt) add_opt(opt, ...)
opt |
The option object returned by ['set_opt()'] or ['setGlobalOptions()']. |
... |
New options. |
'reset_opt()' is identical to 'opt(RESET = TRUE)'.
'add_opt()' is identical to 'opt(..., ADD = TRUE)'.
opt = set_opt(a = 1, b = 2) opt$a = 100; opt$b = 200 opt reset_opt(opt) opt opt = set_opt(a = 1) add_opt(opt, b = 2) optopt = set_opt(a = 1, b = 2) opt$a = 100; opt$b = 200 opt reset_opt(opt) opt opt = set_opt(a = 1) add_opt(opt, b = 2) opt
Option Generator
setGlobalOptions(...) set_opt(...)setGlobalOptions(...) set_opt(...)
... |
Specification of options, see the **Details** section. |
The simplest way is to construct an option function (e.g. 'opt()') as:
“' opt = set_opt( "a" = 1, "b" = "text" ) “'
Then users can get or set the options by
“' opt() opt("a") opt$a opt[["a"]] opt(c("a", "b")) opt("a", "b") opt("a" = 2) opt$a = 2 opt[["a"]] = 2 opt("a" = 2, "b" = "new_text") “'
Options can be reset to their default values by:
“' opt(RESET = TRUE) # or reset_opt(opt) “'
The value for each option can be set as a list which contains more complex configurations:
“' opt = set_opt( "a" = list( .value = 1, .length = 1, .class = "numeric", .validate = function(x) x > 0 ) ) “'
The different fields in the list can be used to filter or validate the option values.
- '.value': The default value. - '.length': The valid length of the option value. It can be a vector, the check will be passed if one of the length fits. - '.class': The valid class of the option value. It can be a vector, the check will be passed if one of the classes fits. - '.validate': Validation function. The input parameter is the option value and should return a single logical value. - '.failed_msg': Once validation failed, the error message that is printed. - '.filter': Filtering function. The input parameter is the option value and it should return a filtered option value. - '.read.only': Logical. The option value can not be modified if it is set to 'TRUE'. - '.visible': Logical. Whether the option is visible to users. - '.private': Logical. The option value can only be modified in the same namespace where the option function is created. - '.synonymous': a single option name which should have been already defined ahead of current option. The option specified will be shared by current option. - '.description': a short text for describing the option. The description is only used when printing the object.
For more detailed explanation, please go to the vignette.
opt = set_opt( a = 1, b = "text" ) opt # for more examples, please go to the vignetteopt = set_opt( a = 1, b = "text" ) opt # for more examples, please go to the vignette