Skip to content

Commit

Permalink
Add glue support with names_glue
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Hanna authored and Richard Hanna committed Aug 5, 2024
1 parent 0f868b8 commit abefbee
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Imports:
stats
Suggests:
covr,
glue,
knitr,
labelled,
lintr,
Expand Down
9 changes: 8 additions & 1 deletion R/combine_checkboxes.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
#' @param names_sep String to separate new column names from `names_prefix`.
#' @param names_glue Instead of `names_sep` and `names_prefix`, you can supply
#' a glue specification and the unique `.value` to create custom column names.
#' @param names_repair What happens if the output has invalid column names?
#' The default, "check_unique" is to error if the columns are duplicated.
#' Use "minimal" to allow duplicates in the output, or "unique" to de-duplicated
#' by adding numeric suffixes. See [vctrs::vec_as_names()] for more options.
#' @param multi_value_label A string specifying the value to be used when multiple
#' checkbox fields are selected. Default "Multiple".
#' @param values_fill Value to use when no checkboxes are selected. Default `NA`.
Expand Down Expand Up @@ -63,6 +67,8 @@ combine_checkboxes <- function(supertbl,
check_arg_is_supertbl(supertbl, req_cols = c("redcap_data", "redcap_metadata"))
check_arg_is_character(names_prefix, len = 1)
check_arg_is_character(names_sep, len = 1, any.missing = TRUE)
check_arg_is_character(names_repair, len = 1, any.missing = FALSE)
check_arg_is_character(names_glue, len = 1, any.missing = FALSE, null.ok = TRUE)
check_arg_is_character(tbl, len = 1, any.missing = FALSE)
check_arg_is_character(multi_value_label, len = 1, any.missing = TRUE)
check_arg_is_character(values_fill, len = 1, any.missing = TRUE)
Expand Down Expand Up @@ -139,6 +145,7 @@ get_metadata_spec <- function(metadata_tbl,

# Create a metadata reference table linking field name to raw and label values
if (!is.null(names_glue)) {
check_installed("glue", reason = "to use `names_glue` in `combine_checkboxes()`")
# Similar to pivot_*, use of `names_glue` overrides use of names_prefix/sep
out <- metadata_tbl %>%
filter(.data$field_name %in% selected_cols) %>%
Expand All @@ -152,7 +159,7 @@ get_metadata_spec <- function(metadata_tbl,
mutate(
.value = sub("___.*$", "", .data$field_name),
.new_value = case_when(names_prefix != "" ~ paste(names_prefix, .value, sep = names_sep),
.default = paste(names_prefix, .data$.value, sep = "")
.default = paste(names_prefix, .data$.value, sep = "")
)
)
}
Expand Down
5 changes: 5 additions & 0 deletions man/combine_and_repair_tbls.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions man/combine_checkboxes.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions tests/testthat/test-combine_checkboxes.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,11 @@ test_that("combine_checkboxes works for nonrepeat instrument", {
})

test_that("combine_checkboxes glue spec works", {
name_appender <- "suffix"

out <- combine_checkboxes(
supertbl = supertbl,
tbl = "nonrepeat_instrument",
cols = starts_with("multi"),
names_glue = "{.value}_{name_appender}",
names_glue = "{.value}_suffix",
multi_value_label = "multiple", # multi_value_label declared
values_fill = "none" # values_fill declared
) %>%
Expand Down Expand Up @@ -149,7 +147,7 @@ test_that("get_metadata_spec works", {
out <- get_metadata_spec(
metadata_tbl = supertbl$redcap_metadata[[1]],
selected_cols = c("multi___1", "multi___2", "multi___3"),
names_prefix = "", names_sep = "_" # Mimic defaults
names_prefix = "", names_sep = "_", names_glue = NULL # Mimic defaults
)

expected_out <- tibble::tribble(
Expand Down

0 comments on commit abefbee

Please sign in to comment.