rgtsummary

Error in tbl_continuous() in gtsummary when using themes


I was making a new theme in gtsummary package, but it seems it affects the tbl_continuous function, although I did not make any arguments regarding tbl_continuous in my theme. Also, I cannot find in the documentations any theme elements specific to tbl_continuous.

Here is my code:

    > my_theme = list(
    +   "pkgwide-str:theme_name" = "JO JO",
    +   "pkgwide-fn:pvalue_fun" = function(x) style_pvalue(x, digits = 2),
    +   "pkgwide-fun:pre_conversion" = function(x) bold_labels(x),
    +   "pkgwide-str:ci.sep" = "-",
    +   #$ tbl_regression $#
    +   "tbl_regression-str:ref_row_text" = "1(r)",
    +   #$ tbl_summary $#
    +   "tbl_summary-fn:percent_fun" = function(x) style_percent(x, symbol = T, digits = 1),
    +   "tbl_summary-arg:statistic" = list(all_continuous() ~"{mean} ({sd})",
    +                                      all_categorical() ~ "{n} ({p})"))
    > check_gtsummary_theme(my_theme)
    ✔ Looks good!
    > set_gtsummary_theme(my_theme)
    Setting theme "JO JO"
    > tbl_continuous(
    +   data = trial,
    +   variable = age,
    +   by = trt,
    +   include = grade
    + )
    Error in `tbl_continuous()`:
    ! Problem with the `statistic` argument.
    Error converting string "p" to a function.
    ℹ Is the name spelled correctly and available?
    Run `rlang::last_trace()` to see where the error occurred.
    > rlang::last_trace()
    <error/rlang_error>
    Error in `tbl_continuous()`:
    ! Problem with the `statistic` argument.
    Error converting string "p" to a function.
    ℹ Is the name spelled correctly and available?
    ---
    Backtrace:
        ▆
     1. └─gtsummary::tbl_continuous(data = trial, variable = age, by = trt, include = grade)
    Run rlang::last_trace(drop = FALSE) to see 27 hidden frames.
    > rlang::last_trace(drop = FALSE)
    <error/rlang_error>
    Error in `tbl_continuous()`:
    ! Problem with the `statistic` argument.
    Error converting string "p" to a function.
    ℹ Is the name spelled correctly and available?
    ---
    Backtrace:
         ▆
      1. └─gtsummary::tbl_continuous(data = trial, variable = age, by = trt, include = grade)
      2.   ├─dplyr::bind_rows(...)
      3.   │ └─rlang::list2(...)
      4.   └─gtsummary:::map(...)
      5.     └─base::lapply(.x, .f, ...)
      6.       └─gtsummary (local) FUN(X[[i]], ...)
      7.         ├─cards::bind_ard(...)
      8.         │ └─dplyr::bind_rows(...)
      9.         │   └─rlang::list2(...)
     10.         ├─cards::ard_continuous(...)
     11.         ├─cards:::ard_continuous.data.frame(...)
     12.         │ ├─cards::process_formula_selectors(...)
     13.         │ └─cards:::process_formula_selectors.data.frame(...)
     14.         │   └─rlang::dots_list(...)
     15.         ├─rlang::set_names(...)
     16.         └─gtsummary:::.continuous_statistics_chr_to_fun(statistic)
     17.           └─base::lapply(...)
     18.             └─gtsummary (local) FUN(X[[i]], ...)
     19.               ├─rlang::set_names(...)
     20.               └─gtsummary:::map2(...)
     21.                 └─base::mapply(.f, .x, .y, MoreArgs = list(...), SIMPLIFY = FALSE)
     22.                   └─gtsummary (local) `<fn>`(dots[[1L]][[2L]], dots[[2L]][[2L]])
     23.                     └─base::tryCatch(...)
     24.                       └─base (local) tryCatchList(expr, classes, parentenv, handlers)
     25.                         └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
     26.                           └─value[[3L]](cond)
     27.                             └─cli::cli_abort(...)
     28.                               └─rlang::abort(...)
    > reset_gtsummary_theme()
    > tbl_continuous(
    +   data = trial,
    +   variable = age,
    +   by = trt,
    +   include = grade
    + )

When I reset the theme, it outputs the table without any problems. I want to know where the problem is and also why there is not theme elements specific to tbl_continuous.

Edit: I also tried using theme_gtsummary_mean_sd() built in gtsummary and the same error persists.

    > theme_gtsummary_mean_sd()
    > trial |> 
    +   tbl_continuous(age)
    Error in `tbl_continuous()`:
    ! Problem with the `statistic` argument.
    Error converting string "p" to a function.
    ℹ Is the name spelled correctly and available?
    Run `rlang::last_trace()` to see where the error occurred.

Solution

  • Thank for posting. There was an error where the tbl_summary statistic theme was being incorrectly applied to tbl_continuous(). The fix is now live in the dev version of the package. https://github.com/ddsjoberg/gtsummary

    library(gtsummary)
    packageVersion("gtsummary")
    #> [1] '2.0.2.9002'
    
    my_theme = list(
      "pkgwide-str:theme_name" = "JO JO",
      "pkgwide-fn:pvalue_fun" = function(x) style_pvalue(x, digits = 2),
      "pkgwide-fun:pre_conversion" = function(x) bold_labels(x),
      "pkgwide-str:ci.sep" = "-",
      # #$ tbl_regression $#
      "tbl_regression-str:ref_row_text" = "1(r)",
      # #$ tbl_summary $#
      "tbl_summary-fn:percent_fun" = function(x) style_percent(x, symbol = T, digits = 1),
      "tbl_summary-arg:statistic" = list(all_continuous() ~"{mean} ({sd})", all_categorical() ~ "{n} ({p})")
    )
    
    check_gtsummary_theme(my_theme)
    #> ✔ Looks good!
    set_gtsummary_theme(my_theme)
    #> Setting theme "JO JO"
    
    tbl_continuous(
      data = trial,
      variable = age,
      by = trt,
      include = grade
    ) |> 
      as_kable()
    
    Characteristic Drug A N = 98 Drug B N = 102
    Grade
    I 46 (36, 60) 48 (42, 55)
    II 45 (31, 55) 51 (42, 58)
    III 52 (42, 61) 45 (36, 52)

    Created on 2024-09-12 with reprex v2.1.0