Skip to content

Commit

Permalink
Move convert_mixed_instrument to distill fx
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Hanna authored and Richard Hanna committed Mar 8, 2024
1 parent 71f34ad commit 64c6280
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
28 changes: 19 additions & 9 deletions R/clean_redcap_long.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ clean_redcap_long <- function(db_data_long,
mixed_structure_ref <- data.frame()

if (allow_mixed_structure) {
# Retrieve mixed structure fields and forms
# Retrieve mixed structure fields and forms in reference df
mixed_structure_ref <- get_mixed_structure_fields(db_data_long) %>%
filter(.data$rep_and_nonrep & !str_ends(.data$field_name, "_form_complete")) %>%
left_join(db_metadata_long %>% select(.data$field_name, .data$form_name),
Expand All @@ -91,14 +91,11 @@ clean_redcap_long <- function(db_data_long,

# Update if project actually has mixed structure
has_mixed_structure_forms <- nrow(mixed_structure_ref) > 0

# Convert mixed instruments to mixed structure format
if (has_mixed_structure_forms) {
db_data_long <- convert_mixed_instrument(db_data_long, mixed_structure_ref)
}
} else {
# Throw error if mixed structure detected and not allowed
check_repeat_and_nonrepeat(db_data_long)
if (!has_mixed_structure_forms) {
check_repeat_and_nonrepeat(db_data_long)
}
}

repeated_forms_tibble <- tibble(
Expand All @@ -109,7 +106,9 @@ clean_redcap_long <- function(db_data_long,
.x,
db_data_long,
db_metadata_long,
linked_arms
linked_arms,
has_mixed_structure_forms = has_mixed_structure_forms,
mixed_structure_ref = mixed_structure_ref
)
),
structure = case_when(
Expand Down Expand Up @@ -268,13 +267,19 @@ distill_nonrepeat_table_long <- function(form_name,
#' \code{REDCapR::redcap_metadata_read()$data}
#' @param linked_arms Output of \code{link_arms}, linking instruments to REDCap
#' events/arms
#' @param has_mixed_structure Whether the instrument under evaluation has a mixed
#' structure. Default `FALSE`.
#' @param name mixed_structure_ref A mixed structure reference dataframe supplied
#' by `get_mixed_structure_fields()`.
#'
#' @keywords internal

distill_repeat_table_long <- function(form_name,
db_data_long,
db_metadata_long,
linked_arms) {
linked_arms,
has_mixed_structure_forms = FALSE,
mixed_structure_ref = NULL) {
has_repeat_forms <- "redcap_repeat_instance" %in% names(db_data_long)

my_record_id <- names(db_data_long)[1]
Expand Down Expand Up @@ -308,6 +313,11 @@ distill_repeat_table_long <- function(form_name,
my_fields <- c(my_fields, "redcap_data_access_group")
}

# If has mixed structure, convert form
if (has_mixed_structure_forms) {
db_data_long <- convert_mixed_instrument(db_data_long, mixed_structure_ref %>% filter(form_name == my_form))
}

# Setup data for loop redcap_arm linking
db_data_long <- db_data_long %>%
add_partial_keys(var = .data$redcap_event_name) %>%
Expand Down
10 changes: 9 additions & 1 deletion man/distill_repeat_table_long.Rd

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

0 comments on commit 64c6280

Please sign in to comment.