Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Mixed Repeat/Nonrepeat Instrument Support #177

Merged
merged 20 commits into from
Mar 11, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move convert_mixed_instrument to distill fx
Richard Hanna authored and Richard Hanna committed Mar 8, 2024
commit 64c62809b39590845189b64c7b8ca4bf04f9e1a4
28 changes: 19 additions & 9 deletions R/clean_redcap_long.R
Original file line number Diff line number Diff line change
@@ -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),
@@ -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(
@@ -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,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine but noting that one implication of this is that convert_mixed_instrument() gets run for every instrument even if it's actually repeating rather than mixed. It works because because mixed_structure_ref gets filtered to 0 rows for repeating instruments and the for loop in convert_mixed_instrument() doesn't run. We do still need to filter() mixed_structure_ref every time to find that out though which could be a performance hit. No need to optimize until it's a problem though

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it? The actual conversion is wrapped in a check on has_mixed_structure_forms so if FALSE (which it would be for the whole map() sequence) then convert_mixed_instrument() wouldn't get run unless I'm missing something.

if (has_mixed_structure_forms) {
db_data_long <- convert_mixed_instrument(db_data_long, mixed_structure_ref %>% filter(form_name == my_form))
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right but if you have mixed and repeating instruments then has_mixed_structure_forms is TRUE and it gets run for all the repeating instruments in addition to the mixed instruments.

mixed_structure_ref = mixed_structure_ref
)
),
structure = case_when(
@@ -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]
@@ -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) %>%
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.