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

Pass n_levels to first function parameter #12

Closed
tsostarics opened this issue Nov 27, 2023 · 0 comments
Closed

Pass n_levels to first function parameter #12

tsostarics opened this issue Nov 27, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@tsostarics
Copy link
Owner

Currently, set_contrasts and related functions can use external contrast coding schemes like so:

foo <- function(n) contr.sum(n)
foo2 <- function(n_levels) contr.sum(n_levels)

# Both of these are fine
set_contrasts(df, varName ~ foo)
set_contrasts(df, varName ~ foo2)

But if the named argument isn't n or n_levels then it doesn't work

foo3 <- function(boo) contr.sum(boo)

# Doesn't work
set_contrasts(df, varName ~ foo3)

In the definition for .bundle_params(), we can add the desired function as an optional argument then extend this block:

n_levels <- length(labels[[1]])
other_args <- rlang::dots_list(...)[['other']]
params <- list(n = n_levels)

such that if a function is passed, we can check whether n exists and if not, look up the name of the first parameter and set n_levels to that. Basically something like:

function_parameters <- names(formals(foo))
par_name <- "n"
if (!"n" %in% function_parameters)
  par_name <- function_parameters[1L]

params <- setNames(list(n_levels), par_name)
@tsostarics tsostarics added the bug Something isn't working label Nov 27, 2023
tsostarics added a commit that referenced this issue Dec 3, 2023
…her open issues. Changed all instances of n_levels to n, addressing #13 and allowing contrast_code to be cleaned up a bit; handled passing of n parameter to function via tryCatch routine in functional_code, fixing #12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant