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 |
Maintainer: | Zuguang Gu <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.2 |
Built: | 2024-10-31 21:08:08 UTC |
Source: | https://github.com/jokergoo/globaloptions |
The .DollarNames method for the GlobalOptionsFun class
## S3 method for class 'GlobalOptionsFun' .DollarNames(x, pattern = "")
## S3 method for class 'GlobalOptionsFun' .DollarNames(x, pattern = "")
x |
the object returned by |
pattern |
pattern, please ignore it. |
This makes the option object looks like a list that it allows
option name completion after $
.
# There is no example NULL
# There is no example NULL
Get value of other options
.v(opt_name, name_is_character = NA)
.v(opt_name, name_is_character = NA)
opt_name |
name of the option, can be quoted or not quoted. |
name_is_character |
whether |
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
.
Zuguang Gu <[email protected]>
opt = set_opt(a = 1, b = function() .v$a*2) opt$b opt(a = 2); opt$b
opt = set_opt(a = 1, b = function() .v$a*2) opt$b opt(a = 2); opt$b
Get a single GlobalOption object
## S3 method for class 'GlobalOptionsFun' x[nm]
## S3 method for class 'GlobalOptionsFun' x[nm]
x |
the option object returned by |
nm |
a single name of the option. |
This function is only used internally.
Zuguang Gu <[email protected]>
opt = set_opt(a = 1, b = "b") opt["a"] opt["b"]
opt = set_opt(a = 1, b = "b") opt["a"] opt["b"]
Get option value by subset operator
## S3 method for class 'GlobalOptionsFun' x[[nm]]
## S3 method for class 'GlobalOptionsFun' x[[nm]]
x |
the option object returned by |
nm |
a single option name. |
opt[["a"]]
is same as opt("a")
or opt$a
.
Zuguang Gu <[email protected]>
opt = set_opt(a = 1) opt[["a"]]
opt = set_opt(a = 1) opt[["a"]]
Set option value by subset operator
## S3 replacement method for class 'GlobalOptionsFun' x[[nm]] <- value
## S3 replacement method for class 'GlobalOptionsFun' x[[nm]] <- value
x |
the option object returned by |
nm |
a single option name. |
value |
the value which is assigned to the option. |
opt[["a"]] = 1
is same as opt("a" = 1)
or opt$a = 1
.
Zuguang Gu <[email protected]>
opt = set_opt(a = 1) opt[["a"]] = 2 opt$a
opt = set_opt(a = 1) opt[["a"]] = 2 opt$a
Get option value by dollar symbol
## S3 method for class 'GlobalOptionsFun' x$nm
## S3 method for class 'GlobalOptionsFun' x$nm
x |
the object returned by |
nm |
a single option name. |
opt$a
is same as opt("a")
.
Zuguang Gu <[email protected]>
opt = set_opt(a = 1) opt$a
opt = set_opt(a = 1) opt$a
Get value of other options
## S3 method for class 'InternalOptionValue' x$nm
## S3 method for class 'InternalOptionValue' x$nm
x |
should always be |
nm |
name of the option |
.v$nm
is basically a short version of .v(nm)
.
Zuguang Gu <[email protected]>
# There is no example NULL
# There is no example NULL
Set option value by dollar symbol
## S3 replacement method for class 'GlobalOptionsFun' x$nm <- value
## S3 replacement method for class 'GlobalOptionsFun' x$nm <- value
x |
the object returned by |
nm |
a single option name. |
value |
the value which is assigned to the option. |
opt$a = 1
is same as opt("a" = 1)
.
Note you cannot reconfigurate the option by assigning a configuration list.
Zuguang Gu <[email protected]>
opt = set_opt(a = 1) opt$a = 2 opt$a
opt = set_opt(a = 1) opt$a = 2 opt$a
Print all fields of a single option
dump_opt(opt, opt_name)
dump_opt(opt, opt_name)
opt |
the option object returned by |
opt_name |
a single name of the option. |
Actually this function is identical to opt[opt_name]
.
opt = set_opt(a = 1, b = "b") dump_opt(opt, "a") dump_opt(opt, "b")
opt = set_opt(a = 1, b = "b") dump_opt(opt, "a") dump_opt(opt, "b")
Option names
## S3 method for class 'GlobalOptionsFun' names(x)
## S3 method for class 'GlobalOptionsFun' names(x)
x |
the option object returned by |
A vector of option names
opt = set_opt( a = 1, b = "text" ) names(opt)
opt = set_opt( a = 1, b = "text" ) names(opt)
Print the GlobalOptionsFun object
## S3 method for class 'GlobalOptionsFun' print(x, ...)
## S3 method for class 'GlobalOptionsFun' print(x, ...)
x |
the option object returned by |
... |
other arguments |
# There is no example NULL
# There is no example NULL
Produce a function which can get or set global options
set_opt(...)
set_opt(...)
... |
all go to |
This is just a short name for setGlobalOptions
.
# There is no example NULL
# There is no example NULL
Produce a function which can get or set global options
setGlobalOptions(...)
setGlobalOptions(...)
... |
specification of options, see 'details' section |
The function has a short name set_opt
.
The most simple 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)
The value for each option can be set as a list which contains more configurations of the option:
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.
Zuguang Gu <[email protected]>
opt = set_opt( a = 1, b = "text" ) opt # for more examples, please go to the vignette
opt = set_opt( a = 1, b = "text" ) opt # for more examples, please go to the vignette