R/formula.R

Defines functions prepare_formula formula_string tidy_strings

tidy_strings <- function() {
    "starts_with|ends_with|matches|any_of|all_of"
}

formula_string <- function(f, xy) {
    xy |>
        select(eval(f)) |>
        colnames()
}

#' @importFrom formula.tools lhs rhs
#' @importFrom glue glue
prepare_formula <- function(f, xy) {
    result <- f
    if (grepl(tidy_strings(), as.character(f))) {
        y_vars <- formula_string(lhs(f), xy)
        x_vars <- formula_string(rhs(f), xy)
        result <- glue("{paste0(y_vars, collapse = '+')} ~ {paste0(x_vars, collapse = '+')}") |>
            as.formula()
    }

    result
}

Try the miniLNM package in your browser

Any scripts or data that you put into this service are public.

miniLNM documentation built on Sept. 14, 2024, 1:08 a.m.