Skip to content

Commit

Permalink
Improving error msg in tbl_survfit() when 2 or more stratifying var…
Browse files Browse the repository at this point in the history
…iables are present (#2118)

* Improving error msg in `tbl_survfit()` when 2 or more stratifying variables are present

* Update tbl_survfit.R
  • Loading branch information
ddsjoberg authored Jan 7, 2025
1 parent 9f5f45f commit 262c829
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: gtsummary
Title: Presentation-Ready Data Summary and Analytic Result Tables
Version: 2.0.4.9007
Version: 2.0.4.9008
Authors@R: c(
person("Daniel D.", "Sjoberg", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-0862-2018")),
Expand Down
11 changes: 8 additions & 3 deletions R/tbl_survfit.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#' Survival table
#'
#' @description
#' Function takes a `survfit` object as an argument, and provides a
#' formatted summary table of the results
#' formatted summary table of the results.
#'
#' No more than one stratifying variable is allowed in each model.
#'
#' @param x (`survfit`, `list`, `data.frame`)\cr
#' a survfit object, list of survfit objects, or a data frame.
Expand Down Expand Up @@ -286,6 +289,7 @@ brdg_survfit <- function(cards,
statistic = "{estimate} ({conf.low}, {conf.high})",
label = NULL,
label_header) {
set_cli_abort_call()
# grab information for the headers -------------------------------------------
df_header_survfit <- cards[[1]] |>
dplyr::filter(!.data$context %in% "attributes") |>
Expand All @@ -307,7 +311,8 @@ brdg_survfit <- function(cards,
if (length(cards_names[[i]]) > 1L) {
cli::cli_abort(
c("The {.fun tbl_survfit} function supports {.fun survival::survfit} objects with no more than one stratifying variable.",
i = "The model is stratified by {.val {cards_names[[i]]}}.")
i = "The model is stratified by {.val {cards_names[[i]]}}."),
call = get_cli_abort_call()
)
}
}
Expand Down Expand Up @@ -413,7 +418,7 @@ brdg_survfit <- function(cards,
.default_survfit_labels <- function(x) {
label <- list()
for (i in seq_along(x)) {
variable_i <- x[[i]]$call$formula |> rlang::f_rhs() |> all.vars()
variable_i <- x[[i]]$call$formula |> rlang::f_rhs() |> all.vars() |> dplyr::first() |> discard(is.na)
if (!is_empty(variable_i)) {
label[[variable_i]] <-
tryCatch(
Expand Down
4 changes: 3 additions & 1 deletion man/tbl_survfit.Rd

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

10 changes: 10 additions & 0 deletions tests/testthat/_snaps/tbl_survfit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# tbl_survfit(x) messaging

Code
tbl_survfit(survival::survfit(survival::Surv(ttdeath, death) ~ trt + grade,
trial), times = c(12, 24))
Condition
Error in `tbl_survfit()`:
! The `tbl_survfit()` function supports `survival::survfit()` objects with no more than one stratifying variable.
i The model is stratified by "trt" and "grade".

11 changes: 11 additions & 0 deletions tests/testthat/test-tbl_survfit.R
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,14 @@ test_that("tbl_survfit(conf.level)", {
style_number()
)
})


test_that("tbl_survfit(x) messaging", {
expect_snapshot(
error = TRUE,
tbl_survfit(
survival::survfit(survival::Surv(ttdeath, death) ~ trt + grade, trial),
times = c(12, 24)
)
)
})

0 comments on commit 262c829

Please sign in to comment.