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

Need to Generalize add_timevars_to_data() #67

Open
jthompson-arcus opened this issue Aug 22, 2024 · 2 comments
Open

Need to Generalize add_timevars_to_data() #67

jthompson-arcus opened this issue Aug 22, 2024 · 2 comments

Comments

@jthompson-arcus
Copy link
Collaborator

add_timevars_to_data <- function(
data
){
stopifnot("[data] should be a data frame" = is.data.frame(data))
missing_new_cols <- required_col_names[!required_col_names %in% names(data)] |>
paste0(collapse = ", ")
if(nchar(missing_new_cols) > 0) stop(
paste0("The following columns are missing while they are required:\n",
missing_new_cols, ".")
)
df <- data |>
dplyr::mutate(
edit_date_time = as.POSIXct(edit_date_time, tz = "UTC"),
event_date = as.Date(event_date),
day = event_date - min(event_date, na.rm = TRUE),
vis_day = ifelse(event_id %in% c("SCR", "VIS", "VISEXT", "VISVAR", "FU1", "FU2"), day, NA),
vis_num = as.numeric(factor(vis_day))-1,
event_name = dplyr::case_when(
event_id == "SCR" ~ "Screening",
event_id %in% c("VIS", "VISEXT", "VISVAR") ~ paste0("Visit ", vis_num),
grepl("^FU[[:digit:]]+", event_id) ~ paste0("Visit ", vis_num, "(FU)"),
event_id == "UN" ~ paste0("Unscheduled visit ", event_repeat),
event_id == "EOT" ~ "EoT",
event_id == "EXIT" ~ "Exit",
form_id %in% c("AE", "CM", "CP", "MH", "MH", "MHTR", "PR", "ST", "CMTR", "CMHMA") ~ "Any visit",
TRUE ~ paste0("Other (", event_name, ")")
),
event_label = dplyr::case_when(
!is.na(vis_num) ~ paste0("V", vis_num),
event_id == "UN" ~ paste0("UV", event_repeat),
TRUE ~ event_name
),
.by = subject_id
) |>
dplyr::arrange(
factor(site_code, levels = order_string(site_code)),
factor(subject_id, levels = order_string(subject_id))
)
if(any(grepl("^Other ", df$event_name))) warning(
"Undefined Events detected. Please verify data before proceeding."
)
df
}

We need to find some way to generalize this function and remove all the hard coding. I don't know if this means we need another sheet/item in the metadata or not.

@jthompson-arcus
Copy link
Collaborator Author

jthompson-arcus commented Aug 22, 2024

@LDSamson what is your process for creating metadata$events?

I think I'm most interested in how you know how many visits to create for a study since the code above is imputing the visit number for each subject.

@LDSamson
Copy link
Collaborator

The length of metadata$events is currently not data-driven but just defined based on the study protocol. The reason for that is so that we can show a consistent small visit timeline in the header widgets, even if there is not data yet:
image

I think that is the only place where the tab metadata$events is used. For all other figures, the events are created data-driven (the code that you show in your post above).

Did that answer your question?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants