Skip to content

Commit

Permalink
Fix order so nonrepeat mixed vals appear
Browse files Browse the repository at this point in the history
TODO Fix structure label assignment in supertibble
  • Loading branch information
Richard Hanna authored and Richard Hanna committed Mar 5, 2024
1 parent 357dc17 commit 9e37396
Showing 1 changed file with 34 additions and 26 deletions.
60 changes: 34 additions & 26 deletions R/clean_redcap_long.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,36 +39,13 @@ clean_redcap_long <- function(db_data_long,
assert_data_frame(db_data_long)
assert_data_frame(db_metadata_long)

if (has_repeat_forms) {
if (allow_mixed_structure) {
db_data_long <- convert_mixed_instrument(db_data_long, db_metadata_long)
mixed_structure_forms <- get_mixed_structure_fields(db_data_long)
has_mixed_structure_forms <- ifelse(any(mixed_structure_forms$rep_and_nonrep), TRUE, has_mixed_structure_forms)
} else {
check_repeat_and_nonrepeat(db_data_long)
}
}

## Repeating Instruments Logic ----
## Repeating Forms Assignment ----
# Needed first to inform nonrepeating forms logic
if (has_repeat_forms) {
repeated_forms <- db_data_long %>%
filter(!is.na(.data$redcap_repeat_instrument)) %>%
pull(.data$redcap_repeat_instrument) %>%
unique()

repeated_forms_tibble <- tibble(
redcap_form_name = repeated_forms,
redcap_data = map(
.data$redcap_form_name,
~ distill_repeat_table_long(
.x,
db_data_long,
db_metadata_long,
linked_arms
)
),
structure = ifelse(has_mixed_structure_forms, "mixed", "repeating")
)
}

## Nonrepeating Instruments Logic ----
Expand Down Expand Up @@ -98,6 +75,35 @@ clean_redcap_long <- function(db_data_long,
structure = "nonrepeating"
)

## Repeating Instruments Logic ----
if (has_repeat_forms) {
# If mixed structure allowed, retrieve mixed structure forms
if (allow_mixed_structure) {
mixed_structure_fields <- get_mixed_structure_fields(db_data_long)
has_mixed_structure_forms <- ifelse(any(mixed_structure_fields$rep_and_nonrep), TRUE, has_mixed_structure_forms) # Update

Check warning on line 83 in R/clean_redcap_long.R

View workflow job for this annotation

GitHub Actions / lint

file=R/clean_redcap_long.R,line=83,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 127 characters.
} else {
check_repeat_and_nonrepeat(db_data_long)
}

if (has_mixed_structure_forms) {
db_data_long <- convert_mixed_instrument(db_data_long, db_metadata_long)
}

repeated_forms_tibble <- tibble(
redcap_form_name = repeated_forms,
redcap_data = map(
.data$redcap_form_name,
~ distill_repeat_table_long(
.x,
db_data_long,
db_metadata_long,
linked_arms
)
),
structure = "repeating" # TODO: Fix this so can be "repeating" / "mixed"
)
}

if (has_repeat_forms) {
rbind(repeated_forms_tibble, nonrepeated_forms_tibble)
} else {
Expand Down Expand Up @@ -254,6 +260,7 @@ distill_repeat_table_long <- function(form_name,
db_data_long,
db_metadata_long,
linked_arms) {

has_repeat_forms <- "redcap_repeat_instance" %in% names(db_data_long)

my_record_id <- names(db_data_long)[1]
Expand Down Expand Up @@ -370,10 +377,11 @@ distill_repeat_table_long <- function(form_name,
#' @keywords internal

convert_mixed_instrument <- function(db_data_long, db_metadata_long) {

mixed_structure_fields <- 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),
by = "field_name"
by = "field_name"

Check warning on line 384 in R/clean_redcap_long.R

View workflow job for this annotation

GitHub Actions / lint

file=R/clean_redcap_long.R,line=384,col=14,[indentation_linter] Indentation should be 6 spaces but is 14 spaces.
)

for (i in seq_len(nrow(mixed_structure_fields))) {
Expand Down

0 comments on commit 9e37396

Please sign in to comment.