-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature Request: Function Callbacks on Options #5
Comments
I'm not sure what you mean by 'function callback'. Could you give an example of the workflow you'd like to see? In the vignette you can see that it is possible to define options managers that restrict values allowed for the user, see under |
What you have under library(settings)
inlist_lower <- function (...) {
.list <- unlist(list(...))
function(x) {
x <- tolower(x)
if (!x %in% .list) {
stop(sprintf("Option value out of range. Allowed values are %s",
paste(.list, collapse = ",")), call. = FALSE)
}
x
}
}
opt <- options_manager(foo="up", bar=2,
.allowed = list(
foo = inlist_lower("up","down"),
bar = inrange(min=0, max=3)
)
)
opt(foo="UP")
opt()
# I'd like opt$foo == "up", but opt$foo == "UP" Essentially, if the My real example has multi-column checks on a data.frame, and if an incomplete data.frame is provided, then the missing columns are added. |
So, I think it now does essentially what you want: # function with some hard-coded checks
# x: the option value that will be passed by the options manager.
my_checker <- function(x){
x <- tolower(x)
if (! x %in% c("bla","yada")){
stop("Red Alert. Red Alert!",call.=FALSE)
}
x
}
# define an options manager
my_options <- options_manager(foo="bla"
, .allowed=list(foo=my_checker)) Let's see the output: my_options(foo="BLA")
my_options("foo")
[1] "bla" Would be grateful if you could give it a whirl. I need to document this, probably in a small 2nd vignette and then it can go to CRAN. To test, you need to clone and build it (my |
That looks good. I haven’t had a chance to test it yet, but I’ll try to in the next few days. The first thing that occurs to me for testing is, how will it handle an option that is supposed to actually be |
should work, especially since latest commit. |
I think the updated checking API is realy useful, but I wonder if you really want to store whole data.frame in an options field. In my idea, options are basically like environment variables: numbers or short strings that influence the behaviour of a programme. If its a data frame, it may as well be |
For the reason to store a whole data.frame, it's a default setting for how to perform many calculations as a coherent set. Most of the options I'm using are scalars. It controls the default method for doing calculations when a single dose of a drug is administered. On to the checking... |
Here are some notes on checking it: Check 1The change appears to break the prior behavior of the library(settings)
my_checker <- function(x){
x <- tolower(x)
if (! x %in% c("bla","yada")){
stop("Red Alert. Red Alert!",call.=FALSE)
}
}
my_options <- options_manager(foo="bla",
.allowed=list(foo=my_checker))
my_options(foo="BLA")
#> Error in .op[vars] <<- L: replacement has length zero
my_options("foo")
#> [1] "bla"
my_options(foo="yada")
#> Error in .op[vars] <- L: replacement has length zero
my_options("foo")
#> [1] "bla"
my_options(foo="bing")
#> Error: Red Alert. Red Alert!
my_options("foo")
#> [1] "bla" To restore the prior behavior would be more complex because you'd have to treat a Check 2When initially setting an option with the library(settings)
my_checker <- function(x){
x <- tolower(x)
if (! x %in% c("bla","yada")){
stop("Red Alert. Red Alert!",call.=FALSE)
}
x
}
my_options <- options_manager(foo="BLA",
.allowed=list(foo=my_checker))
my_options("foo")
#> [1] "BLA"
my_options(foo="BLA")
my_options("foo")
#> [1] "bla" (Note that the first time when Check 3An option that doesn't exist looks the same as an option set to library(settings)
my_checker <- function(x){
if (!is.null(x)) {
x <- tolower(x)
if (! (x %in% c("bla","yada"))) {
stop("Red Alert. Red Alert!",call.=FALSE)
}
}
x
}
my_options <- options_manager(foo=NULL,
.allowed=list(foo=my_checker))
my_options("bar")
#> NULL |
For Check 3 above, I think that something like the following would need to be inserted just above this line: Line 122 in fd4d2bf
|
Thanks Bill, I haven't had a chance to take a good look at this for a while. I'll try to make some time so we can fix everything and try to release next Friday. |
I have written an (unfortunately) option handler within the PKNCA package due to necessity and not really looking around for a better solution as I was doing it. I'd like to move the option-handling functions out of PKNCA (billdenney/pknca#71) so that the PKNCA package is focused on its real intent and the option handling is done by a more appropriate package.
With a first look, the only thing missing from your settings package that I use in PKNCA is the ability to have a function callback that checks, modifies, and sets options.
Would you be interested in a pull request that adds the ability to have function callbacks for option setting? (Or, did I miss the feature in the documentation?)
The text was updated successfully, but these errors were encountered: