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 factor levels to redcap_events col #211

Merged
merged 4 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: REDCapTidieR
Type: Package
Title: Extract 'REDCap' Databases into Tidy 'Tibble's
Version: 1.2.0
Version: 1.2.1
Authors@R: c(
person("Richard", "Hanna", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0009-0005-6496-8154")),
Expand Down Expand Up @@ -36,7 +36,8 @@ Imports:
pillar,
vctrs,
readr,
stats
stats,
forcats
Suggests:
covr,
knitr,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ importFrom(dplyr,select)
importFrom(dplyr,slice)
importFrom(dplyr,summarise)
importFrom(dplyr,ungroup)
importFrom(forcats,fct_inorder)
importFrom(formattable,percent)
importFrom(lobstr,obj_size)
importFrom(lubridate,is.Date)
Expand Down
6 changes: 5 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# REDCapTidieR 1.2.0 (development version)
# REDCapTidieR 1.2.1 (development version)

- For longitudinal REDCap projects, the `redcap_events` column has been updated to give REDCap event factor levels and order for the `redcap_event` and `event_name` columns

# REDCapTidieR 1.2.0

- Added `combine_checkboxes()` analytics function
- Use `combine_checkboxes()` to consolidate multiple checkbox fields in a REDCap data tibble under a single column
Expand Down
1 change: 1 addition & 0 deletions R/REDCapTidieR-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#' left_join mutate pull recode relocate rename right_join row_number rowwise
#' select slice summarise ungroup coalesce cur_column bind_cols first nth n_distinct
#' first distinct
#' @importFrom forcats fct_inorder
#' @importFrom formattable percent
#' @importFrom lobstr obj_size
#' @importFrom lubridate is.difftime is.period is.POSIXt is.Date
Expand Down
10 changes: 9 additions & 1 deletion R/read_redcap.R
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ read_redcap <- function(redcap_uri,
export_survey_fields <- ifelse(is.null(export_survey_fields), TRUE, export_survey_fields)

# Load REDCap Dataset output ----

db_data <- try_redcapr({
redcap_read_oneshot(
redcap_uri = redcap_uri,
Expand Down Expand Up @@ -463,12 +462,21 @@ add_event_mapping <- function(supertbl, linked_arms, repeat_event_types) {
event_info <- linked_arms

if (!is.null(repeat_event_types)) {
# Preserve factor levels post-join by referencing level order from linked_arms
repeat_event_types$redcap_event_name <- factor(repeat_event_types$redcap_event_name,
levels = levels(event_info$unique_event_name),
ordered = TRUE
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would actually not make this ordered. We don't really know if the events in an arbitrary REDCap have meaningful order and using a plain factor will still preserve the order if they are meaningful.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Well, for my purposes downstream in record_status_dashboard() creation I wanted to capture info on the order that events appear in the display. There's no real assumptions (to my knowledge) I can make otherwise when just accepting the supertibble. I believe the order that events come out resembles how they're ordered in the UI.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Confirmed this will work using Prodigy as an example.

> db$redcap_events[[1]]
# A tibble: 1 × 5
  redcap_event      event_name        redcap_arm arm_name repeat_type 
  <ord>             <fct>             <chr>      <chr>    <chr>       
1 infusion_sequence Infusion Sequence 1          Arm 1    nonrepeating
> db$redcap_events[[1]]$event_name
[1] Infusion Sequence
23 Levels: Infusion Sequence Screening & Enrollment Manufacturing LD Chemo Pre-Infusion Infusion 1 Day Follow-Up ... Safety/Meds
> db$redcap_events[[1]]$event_name %>% levels()
 [1] "Infusion Sequence"      "Screening & Enrollment" "Manufacturing"          "LD Chemo"               "Pre-Infusion"          
 [6] "Infusion"               "1 Day Follow-Up"        "3 Day Follow-Up"        "7 Day Follow-Up"        "10 Day Follow-Up"      
[11] "14 Day Follow-Up"       "21 Day Follow-Up"       "28 Day Follow-Up"       "2 Month Follow-Up"      "3 Month Follow-Up"     
[16] "4 Month Follow-Up"      "5 Month Follow-Up"      "6 Month Follow-Up"      "9 Month Follow-Up"      "12 Month Follow-Up"    
[21] "Unscheduled"            "End of Study"           "Safety/Meds"    

)

event_info <- event_info %>%
left_join(repeat_event_types, by = c("unique_event_name" = "redcap_event_name"))
}

event_info <- event_info %>%
add_partial_keys(.data$unique_event_name) %>%
mutate(
across(any_of("redcap_event"), ~ fct_inorder(redcap_event, ordered = TRUE))
) %>%
select(
redcap_form_name = "form", "redcap_event", "event_name", "redcap_arm", "arm_name", any_of("repeat_type")
) %>%
Expand Down
6 changes: 5 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,11 @@ link_arms <- function(redcap_uri,
)

left_join(db_event_instruments, arms, by = "arm_num") %>%
left_join(db_event_labels, by = c("arm_num", "unique_event_name"))
left_join(db_event_labels, by = c("arm_num", "unique_event_name")) %>%
mutate(
across(any_of("unique_event_name"), ~ fct_inorder(.x, ordered = TRUE)),
across(any_of("event_name"), ~ fct_inorder(.x, ordered = TRUE))
)
}

#' @title
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@ test_that("link_arms works", {
# all arms are represented in output (test redcap has 2 arms)
n_unique_arms <- length(unique(out$arm_num))
expect_equal(n_unique_arms, 2)
expect_s3_class(out$unique_event_name, "ordered")
expect_s3_class(out$event_name, "ordered")
expect_equal(
levels(out$unique_event_name),
c("event_1_arm_1", "event_2_arm_1", "event_1_arm_2", "event_3_arm_2")
)
})

test_that("update_field_names works", {
Expand Down
Loading