tbl_survfit()When functions add_n() and add_p() are run after tbl_survfit(),
the original call to survival::survfit()
is extracted and the formula= and data= arguments are used to calculate
the N or p-value.
When the values of the formula= and data= are unavailable, the functions
cannot execute. Below are some tips to modify your code to ensure all functions
run without issue.
Let tbl_survfit() construct the survival::survfit() for you by passing
a data frame to tbl_survfit(). The survfit model will be constructed
in a manner ensuring the formula and data are available.
This only works if you have a stratified model.
Instead of the following line
survfit(Surv(ttdeath, death) ~ trt, trial) %>%
  tbl_survfit(times = c(12, 24))
Use this code
trial %>%
  select(ttdeath, death, trt) %>%
  tbl_survfit(y = Surv(ttdeath, death), times = c(12, 24))
Construct an expression of the survival::survfit() before evaluating it.
Ensure the formula and data are available in the call by using the tidyverse
bang-bang operator, !!.
Use this code
formula_arg <- Surv(ttdeath, death) ~ 1
data_arg <- trial
rlang::expr(survfit(!!formula_arg, !!data_arg)) %>%
  eval() %>%
  tbl_survfit(times = c(12, 24))