Skip to content

Commit adf7471

Browse files
committed
Refactor, documentation updates, linting
Refactored #225, moved to new branch to access org secrets to let actions pass. Updated .Rd docs and linting.
1 parent 1292ad4 commit adf7471

File tree

6 files changed

+70
-55
lines changed

6 files changed

+70
-55
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ importFrom(tidyr,fill)
143143
importFrom(tidyr,nest)
144144
importFrom(tidyr,pivot_wider)
145145
importFrom(tidyr,separate_wider_delim)
146+
importFrom(tidyr,unite)
146147
importFrom(tidyr,unnest)
147148
importFrom(tidyr,unnest_wider)
148149
importFrom(tidyselect,all_of)

R/REDCapTidieR-package.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#' @importFrom stringr str_detect str_replace str_replace_all str_squish str_trunc
2727
#' str_trim str_ends
2828
#' @importFrom tibble as_tibble is_tibble tibble
29-
#' @importFrom tidyr complete fill pivot_wider nest separate_wider_delim unnest
29+
#' @importFrom tidyr complete fill pivot_wider nest separate_wider_delim unite unnest
3030
#' unnest_wider
3131
#' @importFrom tidyselect all_of any_of ends_with eval_select everything
3232
#' starts_with where matches

R/combine_checkboxes.R

Lines changed: 53 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@
2929
#' Use "minimal" to allow duplicates in the output, or "unique" to de-duplicated
3030
#' by adding numeric suffixes. See [vctrs::vec_as_names()] for more options.
3131
#' @param multi_value_label A string specifying the value to be used when multiple
32-
#' checkbox fields are selected. Default "Multiple".
33-
#' If `NULL`, multiple selections will be pasted together.
34-
#' @param multi_value_sep (if `multi_value_label` is `NULL`) a string
35-
#' specifying the separator to use to paste multiple selections together.
32+
#' checkbox fields are selected. Default "Multiple". If `NULL`, multiple
33+
#' selections will be pasted together using `multi_value_sep` specification.
34+
#' @param multi_value_sep A string specifying the separator to use to paste
35+
#' multiple selections together when `multi_value_label` is `NULL`. Default
36+
#' `", "`.
3637
#' @param values_fill Value to use when no checkboxes are selected. Default `NA`.
3738
#' @param raw_or_label Either 'raw' or 'label' to specify whether to use raw coded
3839
#' values or labels for the options. Default 'label'.
@@ -153,7 +154,10 @@ combine_checkboxes <- function(supertbl,
153154
nest(.by = ".new_value", .key = "metadata") %>%
154155
pmap(convert_checkbox_vals,
155156
data_tbl = data_tbl_mod,
156-
raw_or_label = raw_or_label, multi_value_label = multi_value_label, multi_value_sep = multi_value_sep, values_fill = values_fill
157+
raw_or_label = raw_or_label,
158+
multi_value_label = multi_value_label,
159+
multi_value_sep = multi_value_sep,
160+
values_fill = values_fill
157161
)
158162

159163
final_tbl <- combine_and_repair_tbls(data_tbl, data_tbl_mod, new_cols, names_repair = names_repair)
@@ -266,7 +270,8 @@ replace_true <- function(col, col_name, metadata, raw_or_label) {
266270
#'
267271
#' `case_when` logic helps determine whether the value is a coalesced singular
268272
#' value or a user-specified one via `multi_value_label` or `values_fill`.
269-
#' If `multi_value_label` is `NULL`, multiple checkbox selections are pasted together.
273+
#' If `multi_value_label` is `NULL`, multiple checkbox selections are pasted
274+
#' together using `multi_value_sep` specification.
270275
#'
271276
#' @details
272277
#' This function is used in conjunction with `pmap()`.
@@ -277,44 +282,50 @@ replace_true <- function(col, col_name, metadata, raw_or_label) {
277282
#' @param data_tbl The data tibble from the original supertibble
278283
#' @param .new_value The new column values made by [combine_checkboxes()]
279284
#' @inheritParams combine_checkboxes
280-
convert_checkbox_vals <- function(metadata, .new_value, data_tbl, raw_or_label, multi_value_label, values_fill, multi_value_sep) {
281-
if (!is.null(multi_value_label)) {
282-
tibble(
283-
!!.new_value := rowSums(!is.na(data_tbl[names(data_tbl) %in% metadata$field_name]))
285+
convert_checkbox_vals <- function(metadata,
286+
.new_value,
287+
data_tbl,
288+
raw_or_label,
289+
multi_value_label,
290+
values_fill,
291+
multi_value_sep) {
292+
use_multi_value_label <- !is.null(multi_value_label)
293+
multi_value_label <- if (use_multi_value_label) multi_value_label else NA_character_
294+
295+
out <- data_tbl %>%
296+
unite(
297+
".combined",
298+
any_of(metadata$field_name),
299+
sep = multi_value_sep,
300+
na.rm = TRUE,
301+
remove = FALSE
284302
) %>%
285-
mutate(
286-
!!.new_value := case_when(. > 1 ~ multi_value_label,
287-
. == 1 ~ coalesce(!!!data_tbl[, names(data_tbl) %in% metadata$field_name]),
288-
.default = values_fill
289-
),
290-
!!.new_value := factor(!!sym(.new_value),
291-
levels = c(metadata[[raw_or_label]], multi_value_label, values_fill)
303+
mutate(
304+
.rowsum = rowSums(!is.na(data_tbl[names(data_tbl) %in% metadata$field_name])),
305+
!!.new_value := case_when(
306+
.rowsum > 1 & !use_multi_value_label ~ .combined,
307+
.rowsum > 1 & use_multi_value_label ~ multi_value_label,
308+
.rowsum == 1 ~ coalesce(!!!data_tbl[, names(data_tbl) %in% metadata$field_name]),
309+
.default = values_fill
310+
),
311+
!!.new_value := !!sym(.new_value) %>%
312+
factor(
313+
levels = metadata[[raw_or_label]] %>%
314+
union(
315+
if (use_multi_value_label) multi_value_label else character(0)
316+
) %>%
317+
union(
318+
setdiff(
319+
unique(.data[[.new_value]]),
320+
values_fill
321+
)
322+
) %>%
323+
union(values_fill)
292324
)
293-
)
294-
} else {
295-
data_tbl %>%
296-
tidyr::unite(
297-
!!.new_value,
298-
any_of(metadata$field_name),
299-
sep = multi_value_sep,
300-
na.rm = TRUE
301-
) %>%
302-
mutate(
303-
!!.new_value := !!sym(.new_value) %>%
304-
dplyr::case_match(
305-
"" ~ values_fill,
306-
.default = !!sym(.new_value)
307-
),
308-
!!.new_value := !!sym(.new_value) %>%
309-
factor(
310-
levels =
311-
metadata[[raw_or_label]] %>%
312-
union(setdiff(!!sym(.new_value), values_fill)) %>%
313-
union(values_fill)
314-
)
315-
) %>%
316-
select(all_of(.new_value))
317-
}
325+
) %>%
326+
select(all_of(.new_value))
327+
328+
out
318329
}
319330

320331
#' @title Combine checkbox fields with respect to repaired outputs

man/combine_checkboxes.Rd

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/convert_checkbox_vals.Rd

Lines changed: 7 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-combine_checkboxes.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,8 @@ test_that("convert_checkbox_vals works()", {
296296

297297
out <- convert_checkbox_vals(
298298
metadata = metadata, .new_value = "_multi", data_tbl = data_tbl,
299-
raw_or_label = "label", multi_value_label = "multi", values_fill = NA
299+
raw_or_label = "label", multi_value_label = "multi", values_fill = NA,
300+
multi_value_sep = ", "
300301
)
301302

302303
expected_out <- tibble(

0 commit comments

Comments
 (0)