Skip to content

Commit

Permalink
Merge 2b805a2 into edb15c2
Browse files Browse the repository at this point in the history
  • Loading branch information
ddsjoberg authored Jun 13, 2024
2 parents edb15c2 + 2b805a2 commit 0fd32ce
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 1 deletion.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export(ard_hierarchical)
export(ard_hierarchical_count)
export(ard_missing)
export(ard_stack)
export(ard_total_n)
export(as_nested_list)
export(bind_ard)
export(cards_select)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# cards 0.1.0.9037

* Added new function `ard_total_n()` for calculating the total N in a data frame. (#236)

* The API for `ard_continuous(statistic)` and `ard_missing(statistic)` arguments has been updated. Previously, the RHS of these argument's passed lists would be either `continuous_summary_fns()` and `missing_summary_fns()`. Now these arguments accept simple character vectors of the statistic names. For example, `ard_categorical(statistic = everything() ~ c("n", "p", "N"))` and `ard_missing(statistic = everything() ~ c("N_obs", "N_miss", "N_nonmiss", "p_miss", "p_nonmiss"))`. (#223)

* Added the `nest_for_ard(include_data)` argument to either include or exclude the subsetted data frames in a list-column in the returned tibble.
Expand Down
14 changes: 13 additions & 1 deletion R/ard_stack.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
#' @param .shuffle (`logical`)\cr
#' logical indicating whether to perform `shuffle_ard()` on the final result.
#' Default is `FALSE`.
#' @param .total_n (`logical`)\cr
#' logical indicating whether to include of `ard_total_n()` in the returned ARD.
#'
#' @return a transformed ARD data frame (of class 'card' if `.shuffle = FALSE`)
#'
#' @export
#'
#' @examples
#' ard_stack(
#' data = ADSL,
Expand All @@ -55,6 +57,7 @@ ard_stack <- function(data,
.overall = FALSE,
.missing = FALSE,
.attributes = FALSE,
.total_n = FALSE,
.shuffle = FALSE) {
set_cli_abort_call()

Expand All @@ -69,6 +72,7 @@ ard_stack <- function(data,
check_scalar_logical(.missing)
check_scalar_logical(.attributes)
check_scalar_logical(.shuffle)
check_scalar_logical(.total_n)

if (is_empty(.by) && isTRUE(.overall)) {
cli::cli_inform(
Expand Down Expand Up @@ -128,6 +132,14 @@ ard_stack <- function(data,
)
}

# total n
if (isTRUE(.total_n)) {
ard_full <- bind_ard(
ard_full,
ard_total_n(data)
)
}

# order
ard_full <- tidy_ard_row_order(ard_full)

Expand Down
24 changes: 24 additions & 0 deletions R/ard_total_n.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#' ARD Total N
#'
#' Returns the total N for the data frame.
#' The placeholder variable name returned in the object is `"..ard_total_n.."`
#'
#' @inheritParams ard_dichotomous
#'
#' @return an ARD data frame of class 'card'
#' @export
#'
#' @examples
#' ard_total_n(ADSL)
ard_total_n <- function(data) {
# process inputs -------------------------------------------------------------
set_cli_abort_call()
check_data_frame(data)

# calculate total N ----------------------------------------------------------
data |>
dplyr::mutate(..ard_total_n.. = TRUE) |>
ard_dichotomous(variables = "..ard_total_n..", statistic = list(..ard_total_n.. = "N")) |>
dplyr::mutate(context = "total_n") |>
dplyr::select(-all_ard_variables("levels"))
}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ reference:
- ard_hierarchical
- ard_dichotomous
- ard_missing
- ard_total_n
- ard_complex
- ard_stack

Expand Down
4 changes: 4 additions & 0 deletions man/ard_stack.Rd

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

22 changes: 22 additions & 0 deletions man/ard_total_n.Rd

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

16 changes: 16 additions & 0 deletions tests/testthat/_snaps/ard_total_n.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# ard_total_n() works

Code
as.data.frame(ard_total_n(ADSL))
Output
variable context stat_name stat_label stat fmt_fn warning error
1 ..ard_total_n.. total_n N N 254 0 NULL NULL

---

Code
ard_total_n(letters)
Condition
Error in `ard_total_n()`:
! The `data` argument must be class <data.frame>, not a character vector.

13 changes: 13 additions & 0 deletions tests/testthat/test-ard_stack.R
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,19 @@ test_that("ard_stack() .shuffle argument", {
)
})

test_that("ard_stack() adding total N", {
expect_equal(
ard_stack(
mtcars,
.by = am,
ard_continuous(variables = mpg),
.total_n = TRUE
) |>
tail(n = 1) |>
dplyr::select(-all_ard_groups(), -all_ard_variables("levels")),
ard_total_n(mtcars)
)
})

test_that("ard_stack() works with namespaced functions", {
expect_equal(
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-ard_total_n.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
test_that("ard_total_n() works", {
expect_snapshot(
ard_total_n(ADSL) |>
as.data.frame()
)

expect_snapshot(
error = TRUE,
ard_total_n(letters)
)
})

0 comments on commit 0fd32ce

Please sign in to comment.