tests/testthat/test-add_overall.tbl_svysummary.R

skip_on_cran()
skip_if_not(is_pkg_installed("survey", reference_pkg = "gtsummary"))

svy_mtcars <- survey::svydesign(~1, data = mtcars, weights = ~1)

test_that("add_overall.tbl_svysummary() works", {
  # typical usage works
  expect_error(
    res <-
      svy_mtcars |>
      tbl_svysummary(
        by = am,
        statistic = list(cyl = "{n}", mpg = "{mean}"),
        include = c(cyl, mpg, disp)
      ) |>
      add_overall(),
    NA
  )
  expect_equal(
    res |>
      as.data.frame(col_labels = FALSE) |>
      dplyr::select(-all_stat_cols(), stat_0),
    svy_mtcars |>
      tbl_svysummary(
        include = c(cyl, mpg, disp),
        statistic = list(cyl = "{n}", mpg = "{mean}")
      ) |>
      as.data.frame(col_labels = FALSE)
  )
  # check default header is correct
  expect_equal(
    res$table_styling$header |>
      dplyr::filter(column == "stat_0") |>
      dplyr::pull(label),
    "**Overall**  \nN = 32"
  )

  # we're able to modify the statistic and digits arguments
  expect_error(
    res <-
      svy_mtcars |>
      tbl_svysummary(
        by = am,
        include = c(cyl, mpg, disp)
      ) |>
      add_overall(
        statistic = list(cyl = "{n}", mpg = "{mean}"),
        digits = mpg ~ 4
      ),
    NA
  )
  expect_equal(
    res |>
      as.data.frame(col_labels = FALSE) |>
      dplyr::select(-all_stat_cols(), stat_0),
    svy_mtcars |>
      tbl_svysummary(
        include = c(cyl, mpg, disp),
        statistic = list(cyl = "{n}", mpg = "{mean}"),
        digits = mpg ~ 4
      ) |>
      as.data.frame(col_labels = FALSE)
  )

  # we can change the column header and move the overall column
  expect_error(
    res <-
      svy_mtcars |>
      tbl_svysummary(by = am) |>
      add_overall(last = TRUE, col_label = "**All Transmissions**"),
    NA
  )
  # check the overall column is moved to the end
  expect_equal(
    res |>
      as.data.frame(col_label = FALSE) |>
      names() |>
      dplyr::last(),
    "stat_0"
  )
  # check header is correct
  expect_equal(
    res$table_styling$header |>
      dplyr::filter(column == "stat_0") |>
      dplyr::pull(label),
    "**All Transmissions**"
  )
})


test_that("add_overall.tbl_svysummary() errors", {
  # no stratifying variable
  expect_snapshot(
    error = TRUE,
    tbl_svysummary(svy_mtcars) |> add_overall()
  )

  # Run add_overall() after after `add_stat_label()`
  expect_snapshot(
    error = TRUE,
    tbl_svysummary(svy_mtcars, by = am, include = "mpg", type = all_continuous() ~ "continuous2") |>
      add_stat_label(label = mpg ~ "UPDATED!") |>
      add_overall()
  )
})

Try the gtsummary package in your browser

Any scripts or data that you put into this service are public.

gtsummary documentation built on Oct. 5, 2024, 1:06 a.m.