Skip to content

Commit

Permalink
Allow numeric input to count_by in analyze_num_patients (#1141)
Browse files Browse the repository at this point in the history
Fixes #1062
  • Loading branch information
edelarua authored Nov 28, 2023
1 parent 2203bc7 commit 16dc1f0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 20 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* Added `x_lab` parameter to `g_lineplot` to customize x-axis label.
* Remove 25% padding of y-axis in `g_lineplot`.
* Added support for creating multiple risk difference columns, each comparing to a single comparison group. Multiple comparison groups can be specified as a vector via the `arm_y` argument.
* Allowed numeric vector as `count_by` input in `analyze_num_patients` and `summarize_num_patients`.

### Bug Fixes
* Fixed bug in `decorate_grob` preventing text wrapping from accounting for font size.
Expand Down
14 changes: 5 additions & 9 deletions R/summarize_num_patients.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#' Count the number of unique and non-unique patients in a column (variable).
#'
#' @inheritParams argument_convention
#' @param count_by (`character` or `factor`)\cr optional vector to be combined with `x` when counting
#' `nonunique` records.
#' @param count_by (`vector`)\cr optional vector of any type to be combined with `x` when counting `nonunique`
#' records.
#' @param unique_count_suffix (`logical`)\cr should `"(n)"` suffix be added to `unique_count` labels.
#' Defaults to `TRUE`.
#' @param .stats (`character`)\cr statistics to select for the table. Run `get_stats("summarize_num_patients")`
Expand Down Expand Up @@ -35,7 +35,7 @@ NULL
#' x = as.character(c(1, 1, 1, 2, 4, NA)),
#' labelstr = "",
#' .N_col = 6L,
#' count_by = as.character(c(1, 1, 2, 1, 1, 1))
#' count_by = c(1, 1, 2, 1, 1, 1)
#' )
#'
#' @export
Expand All @@ -51,7 +51,6 @@ s_num_patients <- function(x, labelstr, .N_col, count_by = NULL, unique_count_su

if (!is.null(count_by)) {
checkmate::assert_vector(count_by, len = length(x))
checkmate::assert_multi_class(count_by, classes = c("factor", "character"))
count2 <- n_available(unique(interaction(x, count_by)))
}

Expand Down Expand Up @@ -86,7 +85,7 @@ s_num_patients <- function(x, labelstr, .N_col, count_by = NULL, unique_count_su
#'
#' df_by_event <- data.frame(
#' USUBJID = as.character(c(1, 2, 1, 4, NA)),
#' EVENT = as.character(c(10, 15, 10, 17, 8))
#' EVENT = c(10, 15, 10, 17, 8)
#' )
#' s_num_patients_content(df_by_event, .N_col = 5, .var = "USUBJID", count_by = "EVENT")
#'
Expand All @@ -112,10 +111,7 @@ s_num_patients_content <- function(df,
}

x <- df[[.var]]
y <- switch(as.numeric(!is.null(count_by)) + 1,
NULL,
df[[count_by]]
)
y <- if (is.null(count_by)) NULL else df[[count_by]]

s_num_patients(
x = x,
Expand Down
8 changes: 4 additions & 4 deletions man/summarize_num_patients.Rd

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

14 changes: 7 additions & 7 deletions tests/testthat/test-summarize_num_patients.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ testthat::test_that("summarize_num_patients works as expected with healthy input

testthat::test_that("s_num_patients count_by works as expected with healthy input", {
x <- as.character(c(1, 2, 1, 4, 1))
y <- as.character(c(6, 7, 8, 9, 6))
y <- c(6, 7, 8, 9, 6)
result <- s_num_patients(x = x, labelstr = "", .N_col = 5, count_by = y)

res <- testthat::expect_silent(result)
Expand All @@ -92,7 +92,7 @@ testthat::test_that("s_num_patients count_by works as expected with healthy inpu

testthat::test_that("s_num_patients count_by with missing works as expected with healthy input", {
x <- as.character(c(1, 2, 1, 4, NA))
y <- as.character(c(6, 7, 8, 9, 6))
y <- c(6, 7, 8, 9, 6)
result <- s_num_patients(x = x, labelstr = "", .N_col = 5, count_by = y)

res <- testthat::expect_silent(result)
Expand All @@ -101,7 +101,7 @@ testthat::test_that("s_num_patients count_by with missing works as expected with

testthat::test_that("s_num_patients count_by with missing case 2 works as expected with healthy input", {
x <- as.character(c(1, 2, 1, 4, 1))
y <- as.character(c(6, 7, NA, 9, 6))
y <- c(6, 7, NA, 9, 6)
result <- s_num_patients(x = x, labelstr = "", .N_col = 5, count_by = y)

res <- testthat::expect_silent(result)
Expand All @@ -111,7 +111,7 @@ testthat::test_that("s_num_patients count_by with missing case 2 works as expect
testthat::test_that("s_num_patients_content with count_by works as expected with healthy input", {
df <- data.frame(
USUBJID = as.character(c(1, 2, 1, 4, NA)),
AGE = as.character(c(10, 15, 10, 17, 8))
AGE = c(10, 15, 10, 17, 8)
)
result <- s_num_patients_content(df = df, .N_col = 5, .var = "USUBJID", count_by = "AGE")

Expand All @@ -122,7 +122,7 @@ testthat::test_that("s_num_patients_content with count_by works as expected with
testthat::test_that("s_num_patients_content with count_by case 2 works as expected with healthy input", {
df <- data.frame(
USUBJID = as.character(c(1, 2, 1, 4, NA)),
AGE = as.character(c(10, 15, 11, 17, 8))
AGE = c(10, 15, 11, 17, 8)
)
result <- s_num_patients_content(df = df, .N_col = 5, .var = "USUBJID", count_by = "AGE")

Expand All @@ -133,7 +133,7 @@ testthat::test_that("s_num_patients_content with count_by case 2 works as expect
testthat::test_that("s_num_patients_content with count_by trivial cases, identical to without count_by", {
df <- data.frame(
USUBJID = as.character(c(1, 2, 1, 4, 9)),
AGE = as.character(c(10, 15, 11, 17, 8))
AGE = c(10, 15, 11, 17, 8)
)
result <- s_num_patients_content(df = df, .N_col = 5, .var = "USUBJID", count_by = "USUBJID")

Expand All @@ -145,7 +145,7 @@ testthat::test_that("summarize_num_patients with count_by works as expected with
df <- data.frame(
USUBJID = as.character(c(1, 2, 1, 4, NA, 6, 6, 8, 9)),
ARM = c("A", "A", "A", "A", "A", "B", "B", "B", "B"),
BY = as.character(c(10, 15, 10, 17, 8, 11, 11, 19, 17))
BY = c(10, 15, 10, 17, 8, 11, 11, 19, 17)
)

# Check with both output
Expand Down

0 comments on commit 16dc1f0

Please sign in to comment.