Skip to content

Commit

Permalink
Add more labelling options to count_patients_with_flags
Browse files Browse the repository at this point in the history
  • Loading branch information
edelarua committed Jul 7, 2023
1 parent 0dbcccd commit 5960db8
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 3 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Started deprecation cycle for `summarize_vars` and `control_summarize_vars`. Renamed into `analyze_vars` and `control_analyze_vars` to reflect underlying `rtables` machinery while keeping backward compatibility with aliases.
* Added `ylim` argument to `g_km` to allow the user to set custom limits for the y-axis.
* Added assertion to `g_km` which checks whether there is one arm present in the data when `annot_coxph` is true.
* Added `flag_labels` argument to `s_count_patients_with_flags` to enable more label handling options in `count_patients_by_flags`.

### Miscellaneous
* Began deprecation of `time_unit_input` and `time_unit_output` arguments and replaced them with the `input_time_unit` and `num_pt_year`, respectively, in `control_incidence_rate`.
Expand Down
22 changes: 19 additions & 3 deletions R/count_patients_with_flags.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ NULL
#' @param .var (`character`)\cr name of the column that contains the unique identifier.
#' @param flag_variables (`character`)\cr a character vector specifying the names of `logical`
#' variables from analysis dataset used for counting the number of unique identifiers.
#' @param flag_labels (`character`)\cr vector of labels to use for flag variables.
#'
#' @note If `flag_labels` is not specified, variables labels will be extracted from `df`. If variables are not
#' labelled, variable names will be used instead. Alternatively, a named `vector` can be supplied to
#' `flag_variables` such that within each name-value pair the name corresponds to the variable name and the value is
#' the label to use for this variable.
#'
#' @return
#' * `s_count_patients_with_flags()` returns the count and the fraction of unique identifiers with each particular
Expand Down Expand Up @@ -56,12 +62,22 @@ NULL
s_count_patients_with_flags <- function(df,
.var,
flag_variables,
flag_labels = NULL,
.N_col, # nolint
.N_row, # nolint
denom = c("n", "N_row", "N_col")) {
if (is.null(names(flag_variables))) flag_variables <- stats::setNames(flag_variables, flag_variables)
flag_names <- unname(flag_variables)
flag_variables <- names(flag_variables)
checkmate::assert_character(flag_variables)
if (!is.null(flag_labels)) {
checkmate::assert_character(flag_labels, len = length(flag_variables), any.missing = FALSE)
flag_names <- flag_labels
} else {
if (is.null(names(flag_variables))) {
flag_names <- var_labels(df[flag_variables], fill = TRUE)
} else {
flag_names <- unname(flag_variables)
flag_variables <- names(flag_variables)
}
}

checkmate::assert_subset(flag_variables, colnames(df))
temp <- sapply(flag_variables, function(x) {
Expand Down
10 changes: 10 additions & 0 deletions man/count_patients_with_flags.Rd

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

78 changes: 78 additions & 0 deletions tests/testthat/test-count_patients_with_flags.R
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,81 @@ testthat::test_that("count_patients_with_flags works with label row specified",
res <- testthat::expect_silent(result)
testthat::expect_snapshot(res)
})

testthat::test_that("Custom variable label behaviour works", {
adae_local <- tern_ex_adae %>%
dplyr::mutate(
SER = AESER == "Y",
REL = AEREL == "Y",
CTC35 = AETOXGR %in% c("3", "4", "5"),
CTC45 = AETOXGR %in% c("4", "5")
)
columns <- c("SER", "REL", "CTC35", "CTC45")

# No variable labels (variable names used)
lyt <- basic_table() %>%
split_cols_by("ACTARM") %>%
count_patients_with_flags(
"USUBJID",
flag_variables = aesi_vars,
denom = "N_col",
var_labels = "Total number of patients with at least one",
show_labels = "visible"
)
result <- build_table(lyt, df = adae_local, alt_counts_df = tern_ex_adsl)

res <- testthat::expect_silent(result)
testthat::expect_snapshot(res)

labels <- c("Serious AE", "Related AE", "Grade 3-5 AE", "Grade 4/5 AE")
for (i in seq_along(columns)) {
attr(adae_local[[columns[i]]], "label") <- labels[i]
}
aesi_vars <- c("SER", "REL", "CTC35", "CTC45")

# Variable labels from df
lyt <- basic_table() %>%
split_cols_by("ACTARM") %>%
count_patients_with_flags(
"USUBJID",
flag_variables = aesi_vars,
denom = "N_col",
var_labels = "Total number of patients with at least one",
show_labels = "visible"
)
result <- build_table(lyt, df = adae_local, alt_counts_df = tern_ex_adsl)

res <- testthat::expect_silent(result)
testthat::expect_snapshot(res)

# Custom labels via flag_labels argument
lyt <- basic_table() %>%
split_cols_by("ACTARM") %>%
count_patients_with_flags(
"USUBJID",
flag_variables = aesi_vars,
flag_labels = c("Category 1", "Category 2", "Category 3", "Category 4"),
denom = "N_col",
var_labels = "Total number of patients with at least one",
show_labels = "visible"
)
result <- build_table(lyt, df = adae_local, alt_counts_df = tern_ex_adsl)

res <- testthat::expect_silent(result)
testthat::expect_snapshot(res)

# Labels supplied within flag_variables argument
lyt <- basic_table() %>%
split_cols_by("ACTARM") %>%
count_patients_with_flags(
"USUBJID",
flag_variables = formatters::var_labels(adae_local[, aesi_vars]),
denom = "N_col",
var_labels = "Total number of patients with at least one",
show_labels = "visible"
)
result <- build_table(lyt, df = adae_local, alt_counts_df = tern_ex_adsl)

res <- testthat::expect_silent(result)
testthat::expect_snapshot(res)
})

0 comments on commit 5960db8

Please sign in to comment.