Set theme for categorical and continuous variables in gtsummary package for tbl_summary function in R

Viewed 154

I have a question regarding the gtsummary package and how to set a default theme:

DF:

library(tidyverse)
library(gtsummary)
library(gapminder)

gap <- gapminder %>% 
  dplyr::mutate_all(~ifelse(
    sample(c(TRUE, FALSE), size = length(.), replace = TRUE, prob = c(0.8, 0.2)),
    as.character(.), 
    NA)
  ) %>% 
  dplyr::mutate_at(vars(year:gdpPercap), ~as.numeric(.)
  ) 

my_theme <-   
  list(
    "tbl_summary-str:default_con_type" = "continuous2",
    "tbl_summary-str:continuous_stat" = c("{median} ({p25} - {p75})",
                                          "{mean} ({sd})", 
                                          "{min} - {max}",
                                          "{N_miss} ({p_miss}%)"),
    "tbl_summary-str:categorical_stat" = "{n} / {N} ({p}%)",
    "style_number-arg:big.mark" = ""
  )

gtsummary::set_gtsummary_theme(my_theme)

gap[, -1] %>% 
  gtsummary::tbl_summary(
    
    # remove default missing values because we specified them in 
    # the theme above already
    missing = "no"
  )

The above gives me an error when I include the continent as a categorical variable and does not display a table with summary statistics for the continent. Do I have to specify some arguments in the tbl_summary function or do I have to add/remove some arguments in the theme function? Thank you!

1 Answers

Thank you so much for reporting this unexpected behavior. This is indeed a bug, and it has been fixed in the dev version (which you can install from github).

remotes::install_github("ddsjoberg/gtsummary", quiet = TRUE)
library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.3.5.9003'

my_theme <-   
  list(
    "tbl_summary-str:default_con_type" = "continuous2",
    "tbl_summary-str:continuous_stat" = c("{median} ({p25} - {p75})", "{mean} ({sd})"),
    "tbl_summary-str:categorical_stat" = "{n} / {N} ({p}%)"
  )

set_gtsummary_theme(my_theme)

tbl <-
  gapminder::gapminder[, 2:3] %>% 
  tbl_summary()

enter image description here Created on 2020-10-25 by the reprex package (v0.3.0)

Related