Skip to content

Commit

Permalink
Update docs and improve metadata$events column names
Browse files Browse the repository at this point in the history
  • Loading branch information
LDSamson committed Dec 20, 2024
1 parent 6c5c757 commit 5315685
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 22 deletions.
30 changes: 15 additions & 15 deletions R/fct_data_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ rename_raw_data <- function(
#'
#' @param data A data frame
#' @param events A data frame with events. Needs at least the columns
#' `visit_name`, `visit_suffix`, `event_id_pattern`, `is_visit_day`, and
#' `max_n_events`.
#' `event_prefix`, `event_suffix`, `event_id_pattern`, `is_scheduled_visit`,
#' and `expected_events`.
#' @param label_type Character vector to control the type of label set. Variable
#' not yet in use.
#'
Expand All @@ -156,15 +156,15 @@ add_timevars_to_data <- function(
paste0("The following columns are missing while they are required:\n",
missing_new_cols, ".")
)
required_events_cols <- c("visit_name", "visit_suffix", "event_id_pattern",
"is_visit_day", "max_n_events")
required_events_cols <- c("event_prefix", "event_suffix", "event_id_pattern",
"is_scheduled_visit", "expected_events")
if(!all(required_events_cols %in% names(events))){
stop("data in metadata$events needs at least the columns: '",
paste0(required_events_cols, collapse = ","), "'.")

Check warning on line 163 in R/fct_data_helpers.R

View check run for this annotation

Codecov / codecov/patch

R/fct_data_helpers.R#L162-L163

Added lines #L162 - L163 were not covered by tests
}
events$is_visit_day <- sapply(as.logical(events$is_visit_day), isTRUE)
events$is_scheduled_visit <- sapply(as.logical(events$is_scheduled_visit), isTRUE)
all_event_patterns <- paste0(
with(events, event_id_pattern[is_visit_day]),
with(events, event_id_pattern[is_scheduled_visit]),
collapse = "|"
)

Expand All @@ -191,8 +191,8 @@ add_timevars_to_data <- function(
event_id_pattern,
\(x){paste0(all_ids[grepl(x, all_ids)], collapse = ",") }
),
add_visit_number = is_visit_day & (is.na(max_n_events) | max_n_events > 1 ),
add_event_repeat_number = !is_visit_day & (is.na(max_n_events) | max_n_events > 1 )
add_visit_number = is_scheduled_visit & (is.na(expected_events) | expected_events > 1 ),
add_event_repeat_number = !is_scheduled_visit & (is.na(expected_events) | expected_events > 1 )
) |>
expand_columns(columns = "event_id", separator = ",")
cols_to_remove <- c(names(events), "add_visit_number", "add_event_repeat_number")
Expand All @@ -201,20 +201,20 @@ add_timevars_to_data <- function(
dplyr::left_join(events_table, by = "event_id") |>
tidyr::replace_na(
list(
visit_name = "Any visit",
event_prefix = "Any visit",
add_visit_numbers = FALSE,
add_event_repeat_number = FALSE
)
) |>
dplyr::mutate(
event_name = dplyr::case_when(
add_visit_number ~ paste0(visit_name, " ", vis_num),
add_event_repeat_number ~ paste0(visit_name, " ", event_repeat),
.default = visit_name
add_visit_number ~ paste0(event_prefix, " ", vis_num),
add_event_repeat_number ~ paste0(event_prefix, " ", event_repeat),
.default = event_prefix
),
event_name = ifelse(
!is.na(visit_suffix),
paste0(event_name, " (", visit_suffix, ")"),
!is.na(event_suffix),
paste0(event_name, " (", event_suffix, ")"),
event_name
),
event_label = event_label %|_|% dplyr::case_when(
Expand Down Expand Up @@ -340,7 +340,7 @@ get_meta_vars <- function(data = appdata, meta = metadata){
vars <- list()
# add metadata variables:
vars$min_events <- c(
sum(as.numeric(meta$events[["max_n_events"]]), na.rm = TRUE) - 1, 5
sum(as.numeric(meta$events[["expected_events"]]), na.rm = TRUE) - 1, 5
) |>
max()
vars$items <- meta$items_expanded |>
Expand Down
4 changes: 2 additions & 2 deletions R/fct_figures.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ fig_boxplots <- function(
#' Function to create a simple timeline figure using `ggplot2`.
#'
#' @param data Data frame to use.
#' @param events Data frame containing information about all events. Used
#' to create the right labels in the timeline figure.
#' @param min_events Minimum number of events to include in the compact
#' timeline. See also [fig_timeline()].
#'
#' @return A ggplot2 object.
#' @export
Expand Down
6 changes: 5 additions & 1 deletion R/global.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ utils::globalVariables(
"event_id",
"region",
"suffix_names",
"form_type"
"form_type",
"is_scheduled_visit",
"expected_events",
"event_prefix",
"event_suffix"
)
)

Expand Down
Binary file modified data-raw/metadata.xlsx
Binary file not shown.
Binary file modified data/metadata.rda
Binary file not shown.
4 changes: 2 additions & 2 deletions man/add_timevars_to_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/fig_timeline.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified tests/testthat/fixtures/testapp-raw/altered_metadata.xlsx
Binary file not shown.

0 comments on commit 5315685

Please sign in to comment.