Skip to content

Commit

Permalink
Linting
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Hanna authored and Richard Hanna committed Jul 24, 2024
1 parent ed55292 commit c0b3885
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
4 changes: 2 additions & 2 deletions R/checks.R
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ check_values_to_length <- function(col_groups, values_to, call = caller_env()) {
if (length(values_to) < length(names(col_groups))) {
cli_warn(
message = c(
`!` = "Detected fewer {.code values_to} arguments than the number of checkbox fields. Only the first {length(values_to)} will be used."
`!` = "Detected fewer {.code values_to} arguments than the number of checkbox fields. Only the first {length(values_to)} will be used." # nolint line_length_linter
),
class = c("checkbox_value_to_length", "REDCapTidieR_cond")
)
Expand All @@ -734,7 +734,7 @@ check_values_to_length <- function(col_groups, values_to, call = caller_env()) {
if (length(values_to) > length(names(col_groups))) {
cli_abort(
message = c(
`x` = "The number of {.code values_to} arguments supplied is greater than the number of checkbox fields detected.",
`x` = "The number of {.code values_to} arguments supplied is greater than the number of checkbox fields detected.", # nolint line_length_linter
`i` = "{length(values_to)} {.code values_to} supplied, {length(names(col_groups))} checkbox fields detected."
),
class = c("checkbox_value_to_length", "REDCapTidieR_cond")
Expand Down
39 changes: 21 additions & 18 deletions R/combine_checkboxes.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,34 +92,37 @@ combine_checkboxes <- function(supertbl,

# Replace TRUEs/1s with raw/label values from metadata
data_tbl_mod <- data_tbl_mod %>%
mutate(across(
selected_cols,
~ replace_true(.x,
cur_column(),
metadata = metadata_ref,
raw_or_label = raw_or_label
)
),
across(selected_cols, as.character) # enforce to character strings
mutate(
across(
selected_cols,
~ replace_true(.x,
cur_column(),
metadata = metadata_ref,
raw_or_label = raw_or_label
)
),
across(selected_cols, as.character) # enforce to character strings
)

for (i in seq_along(values_to)) {
metadata_overwrite <- metadata_ref %>% filter(field_name %in% col_groups[[i]]) %>% pull(raw_or_label)
metadata_overwrite <- metadata_ref %>%
filter(.data$field_name %in% col_groups[[i]]) %>%
pull(raw_or_label)

data_tbl_mod <- data_tbl_mod %>%
mutate(
!!values_to[i] := ifelse(!!sym(values_to[i]),
multi_value_label,
coalesce(!!!syms(col_groups[[i]]))
multi_value_label,
coalesce(!!!syms(col_groups[[i]]))
),
!!values_to[i] := ifelse(is.na(!!sym(values_to[i])),
values_fill,
!!sym(values_to[i])
values_fill,
!!sym(values_to[i])
)
) %>%
mutate(
!!values_to[i] := factor(!!sym(values_to[i]),
levels = c(metadata_overwrite, multi_value_label, values_fill)
levels = c(metadata_overwrite, multi_value_label, values_fill)
)
)
}
Expand Down Expand Up @@ -166,14 +169,14 @@ get_metadata_ref <- function(metadata_tbl,

for (i in seq_along(unique(out$original_field))) {
index <- unique(out$original_field)[i]
out_filtered <- out %>% filter(original_field == index)
out_filtered <- out %>% filter(.data$original_field == index)

parsed_vals <- rbind(parsed_vals, parse_labels(first(out_filtered$select_choices_or_calculations)))
}

bind_cols(out, parsed_vals) %>%
select(.data$field_name, .data$raw, .data$label, original_field) %>%
relocate(original_field, .after = field_name)
select(.data$field_name, .data$raw, .data$label, .data$original_field) %>%
relocate(.data$original_field, .after = .data$field_name)
}

#' @noRd
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-combine_checkboxes.R
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ test_that("combine_checkboxes works for multiple checkbox fields", {
dplyr::first()

expected_out <- tibble::tribble(
~"study_id",~"extra_data", ~"new_col1", ~"new_col2",
~"study_id", ~"extra_data", ~"new_col1", ~"new_col2",
1, 1, "Red", "Green",
2, 2, "Multiple", "Green",
3, 3, NA, NA
Expand All @@ -186,15 +186,15 @@ test_that("combine_checkboxes works for multiple checkbox fields with logicals",
out <- combine_checkboxes(
supertbl = supertbl,
tbl = "nonrepeat_instrument",
cols = c(starts_with("multi")| starts_with("single_checkbox")),
cols = c(starts_with("multi") | starts_with("single_checkbox")),
values_to = c("new_col1", "new_col2"),
keep = FALSE
) %>%
pull(redcap_data) %>%
dplyr::first()

expected_out <- tibble::tribble(
~"study_id",~"extra_data", ~"new_col1", ~"new_col2",
~"study_id", ~"extra_data", ~"new_col1", ~"new_col2",
1, 1, "Red", "Green",
2, 2, "Multiple", "Green",
3, 3, NA, NA
Expand Down

0 comments on commit c0b3885

Please sign in to comment.