Skip to content
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

Make all arguments (except method and data) optional #24

Open
RoelBrouwer opened this issue Nov 20, 2023 · 4 comments
Open

Make all arguments (except method and data) optional #24

RoelBrouwer opened this issue Nov 20, 2023 · 4 comments
Assignees

Comments

@RoelBrouwer
Copy link
Collaborator

No description provided.

@chStaiger
Copy link
Collaborator

chStaiger commented Nov 23, 2023

@hanneoberman and @stefvanbuuren
We have a problem with the mice function not accepting NULL for the parameters m, seed and maxit.

> mice(nhanes, m=NULL)
Error: Argument m not numeric
> mice(nhanes, seed=NULL)
Error in if (!is.na(seed)) set.seed(seed) : argument is of length zero
> mice(nhanes, maxit=NULL)
Error in array(NA, dim = c(length(varnames), maxit, m)) :
  negative length vectors are not allowed

Because of that we currently hard-coded their values if they are not given in the API call. And of course we risk that the HTTP API will not be synchronised automatically when those values get updated.

Is it possible to
Option 1) Allow for NULL as a value for the three parameters
or
Option 2) have a mice::get_defaults(var_name) function with which we can retrieve the current default values
?

@stefvanbuuren
Copy link
Member

stefvanbuuren commented Nov 24, 2023

Would something like this work?

get_defaults <- function(m, maxit, seed) {
  m <- ifelse(is.null(m), 5, m)
  maxit <- ifelse(is.null(maxit), 5, maxit)
  seed <- ifelse(is.null(seed), NA, seed)
  list(m = m, maxit = maxit, seed = seed)
}

get_defaults(NULL, NULL, NULL)
#> $m
#> [1] 5
#> 
#> $maxit
#> [1] 5
#> 
#> $seed
#> [1] NA
get_defaults(m = NULL, maxit = NULL, seed = NULL)
#> $m
#> [1] 5
#> 
#> $maxit
#> [1] 5
#> 
#> $seed
#> [1] NA

Created on 2023-11-24 with reprex v2.0.2

@chStaiger
Copy link
Collaborator

That looks great. We can also drop the parameters for the function if the return value is a named list with all parameters with a preset value.

@stefvanbuuren
Copy link
Member

Perhaps the function get_defaults() could be improved by returning an alist instead of a list. That would make it easier to work with formals() and do.call().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

When branches are created from issues, their pull requests are automatically linked.

3 participants