From 37b88a811c281d07fca873870ef853b9e9f8aceb Mon Sep 17 00:00:00 2001 From: Richard Hanna Date: Fri, 20 Sep 2024 15:40:01 -0400 Subject: [PATCH] Fix get_repeat_event_types edge case --- R/read_redcap.R | 5 +++-- tests/testthat/test-read_redcap.R | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/R/read_redcap.R b/R/read_redcap.R index f20ff34d..4cb6751f 100644 --- a/R/read_redcap.R +++ b/R/read_redcap.R @@ -544,10 +544,11 @@ get_repeat_event_types <- function(data) { # Check for instances where the same event is labelled as nonrepeating & repeating separate # If this is the case, it must be repeating separate (there is just data that qualifies as both) + out %>% mutate( - is_duplicated = duplicated(.data$repeat_type) | duplicated(.data$repeat_type, fromLast = TRUE) + is_duplicated = (duplicated(.data$redcap_event_name) | duplicated(.data$redcap_event_name, fromLast = TRUE)) ) %>% - filter(.data$is_duplicated == FALSE | (.data$is_duplicated == TRUE & .data$ repeat_type == "repeat_separate")) %>% + filter(!.data$is_duplicated | (.data$is_duplicated & .data$repeat_type == "repeat_separate")) %>% select(-.data$is_duplicated) } diff --git a/tests/testthat/test-read_redcap.R b/tests/testthat/test-read_redcap.R index 194e63f0..769b0d53 100644 --- a/tests/testthat/test-read_redcap.R +++ b/tests/testthat/test-read_redcap.R @@ -660,4 +660,25 @@ test_that("get_repeat_event_types() works", { out <- get_repeat_event_types(mixed_data_structure) expect_equal(out, expected_out) + + # Example with nonrepeating arm that contains repeating and non repeating forms + mixed_data_structure <- tibble::tribble( + ~"record_id", ~"redcap_event_name", ~"redcap_repeat_instrument", ~"redcap_repeat_instance", + 1, "nonrepeat", NA, NA, + 1, "nonrepeat", "repeat_form", 1, + 1, "repeat_together", NA, 1, + 1, "repeat_separate", "mixed_structure_form", 1 + ) + + out <- get_repeat_event_types(mixed_data_structure) + + expected_out <- tibble::tribble( + ~"redcap_event_name", ~"repeat_type", + "nonrepeat", "repeat_separate", + "repeat_together", "repeat_together", + "repeat_separate", "repeat_separate" + ) + + expect_equal(out, expected_out) + })