Search code examples
rgtsummarygt

gtsummary: format digits with comma instead dot


The dots of my numeric values in the tbl_summary table were not all changeable into commas. Only p-values were displayed with a comma. However, all other descriptive statistics and the missing values were yet shown with a dot.

I converted the tbl table into a gt table with as_gt, then added this:

` fmt_number(columns = everything(),
decimals = 2,
dec_mark = ",",
sep_mark = ".")`

Only the p-values appeared with a comma.

Second approach:

  fmt_number(
    columns = starts_with("stat_"),
    decimals = 2,
    dec_mark = ",",
    sep_mark = ".") %>%
  fmt_number(
    columns = matches("p.value|p_val"), 
    decimals = 3,
    dec_mark = ",",
    sep_mark = ".") %>%
  fmt(
    columns = everything(),
    rows = contains("Missing Values"),
    fns = function(x) format(x, big.mark = ".", decimal.mark = ",") )

Again, only the p-values were shown with a comma. For the other statistics, nothing changed.

How can I changes all dots into a comma?


Solution

  • The simplest way to accomplish this for gtsummary tables is by setting a language theme. In the example below, I switched to "es" but you can also keep "en".

    library(gtsummary)
    
    # change the language to spanish and the default big.mark and decimal.mark
    theme_gtsummary_language("es", big.mark = ".", decimal.mark = ",")
    #> Setting theme "language: es"
    
    trial |> 
      tbl_summary(
        by = trt, 
        include = c(marker, response),
        missing = "no"
      ) |> 
      add_difference() |> 
      as_kable()
    
    Característica Drug A N = 98 Drug B N = 102 Difference 95% CI p-valor
    Marker Level (ng/mL) 0,84 (0,23 – 1,60) 0,52 (0,18 – 1,21) 0,20 -0,05 – 0,44 0,12
    Tumor Response 28 (29%) 33 (34%) -4,2% -18% – 9,9% 0,6

    Created on 2024-12-04 with reprex v2.1.1