Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

tbl_survfit_errors

Common Sources of Error with tbl_survfit()


Description

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.

  1. 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))
    
  2. 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))
    

gtsummary

Presentation-Ready Data Summary and Analytic Result Tables

v1.4.0
MIT + file LICENSE
Authors
Daniel D. Sjoberg [aut, cre] (<https://orcid.org/0000-0003-0862-2018>), Michael Curry [aut] (<https://orcid.org/0000-0002-0261-4044>), Margie Hannum [aut] (<https://orcid.org/0000-0002-2953-0449>), Joseph Larmarange [aut] (<https://orcid.org/0000-0001-7097-700X>), Karissa Whiting [aut] (<https://orcid.org/0000-0002-4683-1868>), Emily C. Zabor [aut] (<https://orcid.org/0000-0002-1402-4498>), Esther Drill [ctb] (<https://orcid.org/0000-0002-3315-4538>), Jessica Flynn [ctb] (<https://orcid.org/0000-0001-8310-6684>), Jessica Lavery [ctb] (<https://orcid.org/0000-0002-2746-5647>), Stephanie Lobaugh [ctb], Gustavo Zapata Wainberg [ctb] (<https://orcid.org/0000-0002-2524-3637>)
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.