Skip to content

Commit 6d1cdfc

Browse files
committed
Improve add_pvalue() messaging
1 parent bd7fbc8 commit 6d1cdfc

7 files changed

Lines changed: 58 additions & 2 deletions

File tree

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
* Updates to account for changes in ggplot2 v4.0.0. (#241)
44

5+
* Improved messaging when users pass competing risks models to `add_pvalue()` when more than one outcome is on display.
6+
57
* Added an example to the gallery for combining multiple survival endpoints. (#212)
68

79
* Fixed confidence interval labels being incorrectly swapped for multi-state models in `tidy_survfit()`. Multi-state models now preserve the correct ordering of `conf.low` and `conf.high` from the underlying `broom::tidy()` output. (#215)

R/add_pvalue.R

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ update_add_pvalue <- function(p, add_pvalue_empty_list) {
7878
cli_abort(c("!" = "The {.code add_pvalue(prepend_p=)} argument must be a logical."))
7979
}
8080

81-
8281
# extract survfit object
8382
build <- suppressWarnings(ggplot2::ggplot_build(p))
8483
survfit <- build$plot[["data"]][["survfit"]][[1]]
@@ -98,6 +97,23 @@ update_add_pvalue <- function(p, add_pvalue_empty_list) {
9897
return(p)
9998
}
10099

100+
# check only a single outcome is selected
101+
if (inherits(survfit, "tidycuminc")) {
102+
outcomes_chosen <- suppressWarnings(ggplot2::ggplot_build(p))$plot$data$outcome |> unique()
103+
outcomes_all <- tidycmprsk::tidy(survfit) |>
104+
dplyr::pull("outcome") |>
105+
unique()
106+
outcomes_chosen_position <- which(outcomes_all %in% outcomes_chosen)
107+
if (length(outcomes_chosen) > 1L) {
108+
cli_abort(
109+
c("{.fun add_pvalue} supports reporting a single competing event
110+
p-value and the plot contains {.val {outcomes_chosen}}.",
111+
i = "To place more than one p-value, calculate the p-values with {.code tidycmprsk::tidy},
112+
and insert them into the plot using {.pkg ggplot2} functions.")
113+
)
114+
}
115+
}
116+
101117
# calculate p-value
102118
if (inherits(survfit, "survfit2")) {
103119
p.value <- survfit2_p(survfit,
@@ -108,7 +124,7 @@ update_add_pvalue <- function(p, add_pvalue_empty_list) {
108124
else if (inherits(survfit, "tidycuminc")) {
109125
p.value <-
110126
tidycmprsk::glance(survfit) %>%
111-
dplyr::pull("p.value_1") %>%
127+
dplyr::pull(paste0("p.value_", outcomes_chosen_position)) %>%
112128
pvalue_fun() %>%
113129
{dplyr::case_when(
114130
!prepend_p ~ .,

R/survfit2_p.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ survfit2_p <- function(x, pvalue_fun = format_p, prepend_p = TRUE, rho = 0) {
3838
"i" = "Create a {.cls survfit2} object with {.code survfit2()}.")
3939
)
4040
}
41+
if (inherits(x, "survfitms")) {
42+
cli_abort("The {.fun survfit2_p} does not support multi-state models.")
43+
}
4144

4245
survival::survdiff(
4346
formula = .extract_formula_from_survfit(x),
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# add_pvalue() throws proper errors
2+
3+
Code
4+
tidycmprsk::cuminc(Surv(ttdeath, death_cr) ~ trt, tidycmprsk::trial) %>%
5+
ggcuminc(outcome = c("death other causes", "death from cancer")) + add_pvalue()
6+
Condition
7+
Error in `update_add_pvalue()`:
8+
! `add_pvalue()` supports reporting a single competing event p-value and the plot contains "death from cancer" and "death other causes".
9+
i To place more than one p-value, calculate the p-values with `tidycmprsk::tidy`, and insert them into the plot using ggplot2 functions.
10+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# survfit2_p() throws error
2+
3+
Code
4+
survfit2(Surv(ttdeath, death_cr) ~ trt, tidycmprsk::trial) %>% ggcuminc() +
5+
add_pvalue()
6+
Message
7+
Plotting outcome "death from cancer".
8+
Condition
9+
Error in `survfit2_p()`:
10+
! The `survfit2_p()` does not support multi-state models.
11+

tests/testthat/test-add_pvalue.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,11 @@ test_that("add_pvalue() throws proper errors", {
9292
add_pvalue(prepend_p = letters)) %>%
9393
ggsurvfit_build()
9494
)
95+
96+
expect_snapshot(
97+
error = TRUE,
98+
tidycmprsk::cuminc(Surv(ttdeath, death_cr) ~ trt, tidycmprsk::trial) %>%
99+
ggcuminc(outcome = c("death other causes", "death from cancer")) +
100+
add_pvalue()
101+
)
95102
})

tests/testthat/test-survfit2_p.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,11 @@ test_that("survfit2_p() works", {
3030

3131
test_that("survfit2_p() throws error", {
3232
expect_error(survfit2_p(mtcars))
33+
34+
expect_snapshot(
35+
error = TRUE,
36+
survfit2(Surv(ttdeath, death_cr) ~ trt, tidycmprsk::trial) %>%
37+
ggcuminc() +
38+
add_pvalue()
39+
)
3340
})

0 commit comments

Comments
 (0)