Skip to content

Commit

Permalink
bug!: generate_outlier_subsets() failing when char vec supplied for…
Browse files Browse the repository at this point in the history
… `outcome_variable`

- separated column creation to occur under three conditions: NULL outcome_variable supplied, character string supplied, and expression argument supplied
- separated subset creation to occur separately on results of conditional evaluation
  • Loading branch information
egouldo committed Aug 29, 2024
1 parent ff2e599 commit d391334
Showing 1 changed file with 81 additions and 26 deletions.
107 changes: 81 additions & 26 deletions R/generate_outlier_subsets.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,52 +125,106 @@ generate_outlier_subsets <- function(data, outcome_variable = NULL, n_min = NULL
formulae_match_n_max <- formulae_match(unique(data$dataset), n_min)
}

matched_formulae <- map(outcome_variable,
~ formulae_match(x = names(.x), y = .x))

# ----- Generate Outlier Subsets -----
if (str_detect(data$estimate_type, "Zr") %>% any(na.rm = TRUE)) {

data_Zr <- data %>%
filter(estimate_type == "Zr")

# ---- assign outcome variables ----

if (rlang::is_null(outcome_variable)) {
# NULL value supplied
outcome_variable <- "Zr"

cli::cli_alert_warning(
"Assigning default {.arg outcome_variable} = {.val {outcome_variable}}"
)

data_Zr <- data_Zr %>%
ungroup %>%
mutate(outcome_colname = outcome_variable)

} else if (rlang::is_character(outcome_variable)) {
# Single Value Supplied
stopifnot(length(outcome_variable) == 1)
pointblank::expect_col_exists(data_Zr, columns = {{outcome_variable}})
data_Zr <- data_Zr %>%
ungroup %>%
mutate(outcome_colname = outcome_variable)
}
else{
# expression argument supplied
matched_formulae <- map(outcome_variable,
~ formulae_match(x = names(.x), y = .x))

data_Zr <-
map2(
names(matched_formulae),
matched_formulae,
.f = ~ map_match_formulae(data_Zr, .x, .y)) %>%
bind_rows() %>%
drop_na(outcome_colname)
}

data_Zr <-
map2(
names(matched_formulae),
matched_formulae,
.f = ~ map_match_formulae(data_Zr, .x, .y)) %>%
bind_rows() %>%
drop_na(outcome_colname) %>%
# ----- Generate Outlier Subsets for Zr datasets -----

data_Zr <- data_Zr %>%
map_match_formulae(variable_name = "dataset",
formulae_match_n_min, col_name = "n_min") %>%
formulae_match_n_min,
col_name = "n_min") %>%
map_match_formulae(variable_name = "dataset",
formulae_match_n_max, col_name = "n_max") %>%
formulae_match_n_max,
col_name = "n_max") %>%
apply_slice_conditionally(
x = .,
filter_vars = ignore_subsets) %>%
select(-outcome_colname, -n_min, -n_max)

}

# ---- Generate Outlier Subsets for yi datasets -----
if (str_detect(data$estimate_type, "y") %>%
any(na.rm = TRUE)) {

if (!is.null(enexpr(ignore_subsets))) {
filter_vars <- quos(str_detect(estimate_type, "y"),
!!!rlang::call_args(enquo(ignore_subsets)))
} else {
filter_vars <- quo(str_detect(estimate_type, "y"))
}

data_yi <- data %>%
filter(str_detect(estimate_type, "y"))

# ---- assign outcome variables ----

if (rlang::is_null(outcome_variable)) {
# NULL value supplied
outcome_variable <- "Z"

cli::cli_alert_warning(
"Assigning default {.arg outcome_variable} = {.val {outcome_variable}}"
)

data_yi <- data_yi %>%
ungroup %>%
mutate(outcome_colname = outcome_variable)
} else if (rlang::is_character(outcome_variable)) {
# Single Value Supplied
stopifnot(length(outcome_variable) == 1)
pointblank::expect_col_exists(data_yi, columns = {{outcome_variable}})
data_yi <- data_yi %>%
ungroup %>%
mutate(outcome_colname = outcome_variable)
} else {
# expression argument supplied
matched_formulae <- map(outcome_variable,
~ formulae_match(x = names(.x), y = .x))

data_yi <-
map2(
names(matched_formulae),
matched_formulae,
.f = ~ map_match_formulae(data_yi, .x, .y)) %>%
bind_rows() %>%
drop_na(outcome_colname)
}

data_yi <-
map2(
names(matched_formulae),
matched_formulae,
.f = ~ map_match_formulae(data_yi, .x, .y)) %>%
bind_rows() %>%
drop_na(outcome_colname) %>%
data_yi <- data_yi %>%
map_match_formulae(variable_name = "dataset",
formulae_match_n_min,
col_name = "n_min") %>%
Expand All @@ -181,6 +235,7 @@ generate_outlier_subsets <- function(data, outcome_variable = NULL, n_min = NULL
x = .,
filter_vars = filter_vars) %>%
select(-outcome_colname, -n_min, -n_max)

}

out <- if (exists(x = "data_Zr") & exists(x = "data_yi")) {
Expand Down

0 comments on commit d391334

Please sign in to comment.