Skip to content

Commit

Permalink
Add check_metadata_fields_exist, update details
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Hanna authored and Richard Hanna committed Aug 2, 2024
1 parent 7789a22 commit c185e39
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 1 deletion.
29 changes: 29 additions & 0 deletions R/checks.R
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,35 @@ check_fields_exist <- function(fields, expr, call = caller_env()) {
}
}

#' @title
#' Check metadata fields exist for checkbox combination
#'
#' @description
#' Similar to [check_fields_exist()], but instead of verifying fields that exist
#' in the data tibble this seeks to verify their existence under the metadata
#' tibble `field_name`s.
#'
#' @param metadata_tbl A metadata tibble from the supertibble generated by [read_redcap()].
#' @param cols Selected columns identified for [`combine_checkboxes()`] to be
#' cross checked against `metadata_tibble$field_name`
#' @param call The calling environment to use in the error message
#'
#' @keywords internal
check_metadata_fields_exist <- function(metadata_tbl, cols, call = caller_env()) {
if (!all(cols %in% metadata_tbl$field_name)) {
msg <- c(
x = "Fields detected not present in metadata.",
`!` = "Column{?s} {.code {cols[!cols %in% metadata_tbl$field_name]}} detected as valid in the data tibble, but not found present in the metadata tibble.", # nolint: line_length_linter
`i` = "This may occur if either the names of the data tibble or the metadata tibble `field_name`s were edited."
)

cli_abort(
msg,
class = c("missing_metadata_checkbox_fields", "REDCapTidieR_cond")
)
}
}


#' @title
#' Check fields are of checkbox field type
Expand Down
12 changes: 12 additions & 0 deletions R/combine_checkboxes.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@
#' combining multiple binary columns into a singular and informative labelled
#' factor column.
#'
#' @details
#' [combine_checkboxes()] makes use of the output names of [read_redcap()]
#' data tibbles and metadata tibbles. Changes to checkbox data names or
#' metadata `field_name`s may result in errors.
#'
#' Checkbox fields are expanded to be a variable per checkbox option, separated
#' by underscores. For example, `checkbox_var` with 2 options becomes
#' `checkbox_var___1` and `checkbox_var___2`. [combine_checkboxes()] looks for
#' these and may give a error if it cannot find them.
#'
#' @param supertbl A supertibble generated by [read_redcap()]. Required.
#' @param tbl The `redcap_form_name` of the data tibble to extract. Required.
#' @param cols <[`tidy-select`][tidyr_tidy_select]> Checkbox columns to combine to
Expand Down Expand Up @@ -141,6 +151,8 @@ get_metadata_spec <- function(metadata_tbl,
names_prefix,
names_suffix,
names_sep) {
check_metadata_fields_exist(metadata_tbl, selected_cols)

# Create a metadata reference table linking field name to raw and label values
out <- metadata_tbl %>%
filter(.data$field_name %in% selected_cols) %>%
Expand Down
22 changes: 22 additions & 0 deletions man/check_metadata_fields_exist.Rd

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

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

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

21 changes: 21 additions & 0 deletions tests/testthat/test-checks.R
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,27 @@ test_that("check_fields_exist works", {
expect_no_error()
})

test_that("check_metadata_fields_exist works", {
metadata_valid <- tibble(
field_name = c("var1", "var2", "var_3"),
)
valid_cols <- c("var1", "var2", "var_3")

metadata_invalid <- tibble(
field_name = c("var1", "var2", "var_3_edited"),
)
invalid_cols <- c("var1", "var2", "var_3_edited")

check_metadata_fields_exist(metadata_valid, valid_cols) %>%
expect_no_error()

check_metadata_fields_exist(metadata_invalid, valid_cols) %>%
expect_error(class = "missing_metadata_checkbox_fields")

check_metadata_fields_exist(metadata_valid, invalid_cols) %>%
expect_error(class = "missing_metadata_checkbox_fields")
})

test_that("check_fields_are_checkboxes works", {
metadata <- tibble::tribble(
~field_name, ~field_type,
Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/test-combine_checkboxes.R
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ test_that("convert_metadata_spec works", {
)

out <- convert_metadata_spec(.new_col_item, metadata_spec, data_tbl_mod,
raw_or_label = "label", multi_value_label = "Multiple", values_fill = NA)
raw_or_label = "label", multi_value_label = "Multiple", values_fill = NA
)

expected_out <- tibble::tribble(
~"study_id", ~"multi___1", ~"multi___2", ~"multi___3", ~"single_checkbox___1",
Expand Down

0 comments on commit c185e39

Please sign in to comment.