Skip to content

Commit

Permalink
Add standard checks for params
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Hanna authored and Richard Hanna committed Jul 15, 2024
1 parent 7348324 commit 62080af
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 24 deletions.
35 changes: 22 additions & 13 deletions R/reduce_multi_to_single_column.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
#' factor column.
#'
#' @param supertbl A supertibble generated by [read_redcap()]. Required.
#' @param form_name The name of the REDCap form (instrument) containing the checkbox
#' fields. Required.
#' @param tbl The `redcap_form_name` of the data tibble to extract. Required.
#' @param cols <[`tidy-select`][tidyr_tidy_select]> Checbox columns to combine to
#' single column. Required.
#' @param values_to A string specifying the name of the column to combine checkbox
Expand All @@ -28,7 +27,7 @@
#' supertbl <- read_redcap(redcap_uri, token)
#' combined_tbl <- combine_checkboxes(
#' supertbl = supertbl,
#' form_name = "demographics",
#' tbl = "demographics",
#' cols = starts_with("race"),
#' values_to = "race_combined",
#' multi_value_label = "Multiple",
Expand All @@ -39,27 +38,37 @@
#' @export

combine_checkboxes <- function(supertbl,
form_name,
tbl,
cols,
values_to,
multi_value_label = "Multiple",
values_fill = NA,
raw_or_label = "label",
keep = TRUE) {

# Check args ---
check_arg_is_supertbl(supertbl, req_cols = c("redcap_data", "redcap_metadata"))
check_arg_is_character(tbl, len = 1, any.missing = FALSE)
check_arg_is_character(values_to, 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)
check_arg_choices(raw_or_label, choices = c("label", "raw"))
check_arg_is_logical(keep, len = 1, any.missing = FALSE)

# Save user cols to enquosure
cols_exp <- enquo(cols)

# Extract form_name from supertbl
# Extract tbl from supertbl
data_tbl <- supertbl %>%
extract_tibble(form_name)
extract_tibble(tbl)

# Get field names from cols_exp, check that fields exist
field_names <- names(eval_select(cols_exp, data = data_tbl))
check_fields_exist(fields = field_names, expr = cols_exp)

# Assume the first instrument in the metadata contains IDs
# REDCap enforces this constraints, we reflect this in read_redcap -> get_field_to_drop
record_id_field <- supertbl$redcap_metadata[supertbl$redcap_form_name == form_name][[1]]$field_name[1]
record_id_field <- supertbl$redcap_metadata[supertbl$redcap_form_name == tbl][[1]]$field_name[1]

# Combine record identifier with remaining possible project identifiers
instrument_identifiers <- c(
Expand All @@ -82,7 +91,7 @@ combine_checkboxes <- function(supertbl,
)

# Get metadata reference table, check that chosen fields are checkboxes
metadata <- get_metadata_ref(data_tbl_mod, supertbl, form_name, instrument_identifiers)
metadata <- get_metadata_ref(data_tbl_mod, supertbl, tbl, instrument_identifiers)

# Replace TRUEs/1s with raw/label values from metadata
data_tbl_mod <- data_tbl_mod %>%
Expand Down Expand Up @@ -115,7 +124,7 @@ combine_checkboxes <- function(supertbl,
!!values_to := factor(!!sym(values_to), levels = c(metadata[[raw_or_label]], multi_value_label, values_fill))
)

# Join back onto original data form_name
# Join back onto original data tbl
data_tbl_mod <- data_tbl_mod %>%
right_join(data_tbl, by = intersect(instrument_identifiers, names(data_tbl_mod))) %>%
relocate(!!values_to, .after = everything())
Expand All @@ -127,7 +136,7 @@ combine_checkboxes <- function(supertbl,
}

# Update the supertbl data tibble
supertbl$redcap_data[supertbl$redcap_form_name == form_name][[1]] <- data_tbl_mod
supertbl$redcap_data[supertbl$redcap_form_name == tbl][[1]] <- data_tbl_mod

supertbl
}
Expand All @@ -136,7 +145,7 @@ combine_checkboxes <- function(supertbl,
#'
#' @param data a data tibble
#' @param supertbl A supertibble generated by [read_redcap()].
#' @param form_name The name of the REDCap form (instrument) containing the checkbox
#' @param tbl The name of the REDCap form (instrument) containing the checkbox
#' fields.
#' @param instrument_identifiers Character string vector of project record identifier vars
#'
Expand All @@ -145,10 +154,10 @@ combine_checkboxes <- function(supertbl,
#' @keywords internal
get_metadata_ref <- function(data,
supertbl,
form_name,
tbl,
instrument_identifiers) {
# Create a metadata reference table linking field name to raw and label values
out <- supertbl$redcap_metadata[supertbl$redcap_form_name == form_name][[1]] %>%
out <- supertbl$redcap_metadata[supertbl$redcap_form_name == tbl][[1]] %>%
filter(.data$field_name %in% names(data)[!names(data) %in% instrument_identifiers])

# Make sure selection is checkbox metadata field type
Expand Down
7 changes: 3 additions & 4 deletions man/combine_checkboxes.Rd

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

4 changes: 2 additions & 2 deletions man/get_metadata_ref.Rd

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

10 changes: 5 additions & 5 deletions tests/testthat/test-reduce_multi_to_single_column.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class(supertbl) <- c("redcap_supertbl", class(supertbl))
test_that("combine_checkboxes returns an expected supertbl", {
out <- combine_checkboxes(
supertbl = supertbl,
form_name = "nonrepeat_instrument",
tbl = "nonrepeat_instrument",
cols = starts_with("multi"),
values_to = "new_col"
) # values_fill declared
Expand All @@ -51,7 +51,7 @@ test_that("combine_checkboxes returns an expected supertbl", {
test_that("combine_checkboxes works for nonrepeat instrument", {
out <- combine_checkboxes(
supertbl = supertbl,
form_name = "nonrepeat_instrument",
tbl = "nonrepeat_instrument",
cols = starts_with("multi"),
values_to = "new_col",
multi_value_label = "multiple", # multi_value_label declared
Expand All @@ -76,7 +76,7 @@ test_that("combine_checkboxes works for nonrepeat instrument", {
test_that("combine_checkboxes works for nonrepeat instrument and drop old values", {
out <- combine_checkboxes(
supertbl = supertbl,
form_name = "nonrepeat_instrument",
tbl = "nonrepeat_instrument",
cols = starts_with("multi"),
values_to = "new_col",
keep = FALSE # Test keep = FALSE
Expand All @@ -100,7 +100,7 @@ test_that("combine_checkboxes works for nonrepeat instrument and drop old values
test_that("combine_checkboxes works for repeat instrument", {
out <- combine_checkboxes(
supertbl = supertbl,
form_name = "repeat_instrument",
tbl = "repeat_instrument",
cols = starts_with("repeat"),
values_to = "new_col"
) %>%
Expand Down Expand Up @@ -128,7 +128,7 @@ test_that("get_metadata_ref works", {
out <- get_metadata_ref(
data = data,
supertbl = supertbl,
form_name = "nonrepeat_instrument",
tbl = "nonrepeat_instrument",
instrument_identifiers = "study_id"
)

Expand Down

0 comments on commit 62080af

Please sign in to comment.