# creates table when data frame is passed
expect_snapshot(tbl_summary(data = trial) |> as.data.frame())
expect_snapshot(tbl_summary(data = mtcars) |> as.data.frame())
expect_snapshot(tbl_summary(data = iris) |> as.data.frame())
# errors thrown when bad data argument passed
expect_snapshot(error = TRUE, tbl_summary(data = letters))
expect_snapshot(error = TRUE, tbl_summary(data = dplyr::tibble()))
expect_snapshot(tbl_summary(data = trial, by = trt) |> as.data.frame())
expect_snapshot(tbl_summary(data = mtcars, by = am) |> as.data.frame())
expect_snapshot(tbl_summary(data = iris, by = Species) |> as.data.frame())
expect_snapshot(error = TRUE, tbl_summary(mtcars, by = c("mpg", "am")))
label = list(mpg = "New mpg", cyl = "New cyl"),
tbl_summary(trial["age"], label = list(age = letters))
response ~ "n={n} | N={N} | p={p} | N_obs={N_obs} | N_miss={N_miss} | N_nonmiss={N_nonmiss} | p_miss={p_miss} | p_nonmiss={p_nonmiss}",
# continuous summary, testing cv function and passed in formula
cv <- function(x) sd(x, na.rm = TRUE) / mean(x, na.rm = TRUE) * 100
age ~ "cv={cv} | N_obs={N_obs} | N_miss={N_miss} | N_nonmiss={N_nonmiss} | p_miss={p_miss} | p_nonmiss={p_nonmiss}",
# continuous summary, testing cv function and passed in named list
list(age = "cv={cv} | N_obs={N_obs} | N_miss={N_miss} | N_nonmiss={N_nonmiss} | p_miss={p_miss} | p_nonmiss={p_nonmiss}"),
# we get a nice message for a continuous variable with stat as a character vector
include = c(age, response, marker, ttdeath),
# using named list to change 2 of the 3 statistics
age = list(median = 4, p25 = \(x) style_number(x, digits = 2)),
# using a single integer that will apply to all stats
# passing a single function that will apply to all stats
ttdeath = list(\(x) style_number(x, digits = 2))
median = letters, # this is not a function!
include = c(age, marker, response, stage),
type = list(age = "continuous", marker = "continuous2", response = "dichotomous", state = "categorical"),
# can use the default type to select variables to change the summary type
type = list(all_continuous() ~ "continuous2", all_dichotomous() ~ "continuous"),
list(age = "continuous2", marker = "continuous2", response = "continuous")
# yes/no variables default to dichotomous
data.frame(yn = c("no", "yes", "yes")) |>
yn = c("no", "yes", "yes") |> factor(levels = c("yes", "no"))
yn = c("no", "no", "no") |> factor(levels = c("no", "yes"))
# a yes or no only character defaults to categorical
data.frame(yn = c("nO", "yEs", "yEs")) |>
# a zero or one only numeric defaults to categorical
# grade cannot be summarized continuously, and we'll see reports in the console
# unobserved levels cannot be summarized for a dichotomous summary
# error when no clear dichotomous value present
# ensure grade is coerced to dichotomous and response defaults to dichotomous
tbl <- tbl_summary(trial, value = "grade" ~ "III", include = c(grade, response)),
# check all summary types are assigned to dichotomous
# check we can pass unobserved levels to values
grade = factor(grade, levels = c("I", "II", "III", "IV")),
value = list(grade = "IV", response = FALSE)
tbl_summary(trial, value = "grade" ~ "IV", include = c(grade, response))
# age includes an Unknown row, and trt does not
expect_equal(tbl[, 1], c("Chemotherapy Treatment", "Drug A", "Drug B", "Age", "Unknown"))
# all vars have a missing row when requested
# None of the vars have a missing row when requested
# reporting of non-standard stats works as well
missing_stat = "{N_miss}, {N_obs}, {N_nonmiss}, {p_miss}, {p_nonmiss}"
tbl_summary(trial, include = response, missing_stat = letters)
tbl_summary(trial, include = response, missing_stat = 10L)
tbl_summary(mtcars, sort = all_categorical() ~ "frequency", include = cyl) |>
tbl_summary(mtcars, sort = list(all_categorical() ~ c("frequency", "two")))
tbl_summary(mtcars, sort = list(all_categorical() ~ "freq5555uency"))
tbl_summary(trial, by = trt, include = grade, percent = "column", statistic = ~"{p}%") |>
tbl_summary(trial, by = trt, include = grade, percent = "row", statistic = ~"{p}%") |>
tbl_summary(trial, by = trt, include = grade, percent = "cell", statistic = ~"{p}%") |>
tbl_summary(trial, by = trt, include = grade, percent = letters, statistic = ~"{p}%")