rgtsummary

Is there an easy way to extend number of decimals using gtsummary?


I'm using tbl_summary of the gtsummary package and it's working smooth. Though I realised now that the journal we are planning to publicate in requires small p-values to be reported as <0.0001. Meanwhile gtsummary seems to limit number of digits to be within 1 and 3 and I struggle to make it report with higher accuracy than < 0.001.

Is there an easy way around this? A custom add_stat column (using mostly standard default tests like Wilcoxon rank sum test etc.)?


Solution

  • I got the example below (which I copy/pasted pretty much straight from the documentation) to work, by copying gtsummary::style_pvalue and creating a new function style_pvalue and adding

    else if (digits == 4) {
    p_fmt <- case_when(x > 1 + 1e-15 ~ NA_character_, x < 
    0 - 1e-15 ~ NA_character_, x > 0.9999 ~ paste0(">", 
    style_number(x = 0.9999, digits = 4, big.mark = big.mark, 
    decimal.mark = decimal.mark, ...)), x >= 0.0001 ~ 
    style_number(x, digits = 4, big.mark = big.mark, 
    decimal.mark = decimal.mark, ...), x < 0.0001 ~ 
    paste0("<", style_number(x = 0.0001, digits = 4, big.mark = big.mark, 
    decimal.mark = decimal.mark, ...)))
    }
    

    once I had my new function, I could run:

    my_theme <-
      list(
        # round large p-values to two places
        "pkgwide-fn:pvalue_fun" = function(x) style_pvalue(x, digits = 4),
        "pkgwide-fn:prependpvalue_fun" = function(x) style_pvalue(x, digits = 4, prepend_p = TRUE),
        # report median (IQR) and n (percent) as default stats in `tbl_summary()`
        "tbl_summary-str:continuous_stat" = "{median} ({p25} - {p75})",
        "tbl_summary-str:categorical_stat" = "{n} ({p})"
      )
    
    set_gtsummary_theme(my_theme)
    trial[c("age", "grade", "trt")] %>%
      tbl_summary(by = trt) %>%
      add_p()