rindentationdocxgtsummarygt

gtsave() to docx loses tab_stub_indent in the process


I'm working with gtsummary, and I came across troubles concerning its export as a docx document.

I basically create my table with gt_summary, then convert it to a gt_table which helps me to use some styling options, especially the tab_stub_indent parameter which makes the table much more readable for my categorical variables (to indent them)

When I save the output to a file, I find the HTML file to be the best matching output, however in a docx document, I loose some design parameters.

In the HTML file, the display is perfect, but in docx, I don't have indents anymore. I also tried opening the html file in Excel, but there too I loose the indent.

Do you know if there are any solutions to get the stub indent live through the export ?

Contrary to the vignette about compatibility of exports, I'm concerned that maybe the compatibility isn't fully achieved yet https://www.danieldsjoberg.com/gtsummary/articles/rmarkdown.html

Below is my code: My database typically consists in multiple variables concerning the individual (starting with INDIV_) such as Age, Gender, Ethnicity, Economic group and others concerning the environment (starting with ENV_) such as pollution level (continuous) and exposure to floods (categorical ordered).

library(tidyverse)
library(gtsummary)
library(gt)

filtered_database = data.frame(
  INDIV_AGE = rnorm(100, mean = 50, sd = 4),
  INDIV_GENDER = rbinom(100, size=1, prob = 0.6),
  INDIV_ETHNICS = sample(c("North America", "Western Europe", "Africa", "Eastern Europe", "Asia", "Other"), size = 100, replace = T, prob = c(0.3, 0.2, 0.4, 0.02, 0.01, 0.07)),
  INDIV_ECOGRP = sample(c(1,2,3,4), size = 100, replace = T, prob = c(0.6, 0.1, 0.2, 0.1)),
  ENV_POLLEVEL = rpois(100, lambda = 4), 
  ENV_FLOODPROFILE = sample(c("Low", "Intermediate", "High", "Extreme"), size = 100, replace = T, prob = c(0.1, 0.65, 0.2, 0.05))
)
filtered_database[] <- lapply(filtered_database, function(x) { x[sample(seq_along(x), 0.1 * length(x))] <- NA; x })



a = filtered_database |> 
  tbl_summary(
    include = everything(),
    missing = "always",
    missing_text = "Missing data",
    missing_stat = "{N_miss} ({p_miss}%)",
    type = INDIV_AGE ~ "continuous",
    statistic = list(
      all_continuous() ~ "{median} [{p25}-{p75}]",
      all_categorical() ~ "{n} ({p}%)"
      ),
    by = INDIV_GENDER
  ) |> 
  modify_header(
    label = "",
    stat_2 = "**Yes**\nN={n}",
    stat_1 = "**No**\nN={n}") |>
  modify_spanning_header(all_stat_cols()~"**Gender**") |>
  add_p() |> 
  bold_p() |>
  modify_column_alignment(columns = c("stat_1", "stat_2"), align = "right") |>
  as_gt(rowname_col = "label") |>
  tab_header(
    title = "Description of the population") |> 
  cols_align("right", columns = last_col())

gt_var_names = a$`_data`$variable

b = a |> 
  tab_row_group(
    label = "Individual characteristics",
    rows = str_detect(gt_var_names, "^INDIV_")
  ) |> 
  tab_row_group(
    label = "Environment characteristics",
    rows = str_detect(gt_var_names, "^ENV_")
  ) |>
  row_group_order(groups = c("Environment characteristics", "Individual characteristics")) |> 
  tab_style(
    style = cell_text(weight = "bold"),
    locations = cells_row_groups()
  ) |> 
  tab_style(
    style = cell_text(style = "italic", color = "gray65"),
    locations = cells_body(
      columns = everything(),
      rows = row_type == "missing"
    )
  ) |> 
  tab_style(
    style = cell_text(style = "italic"),
    locations = cells_stub(
      rows = row_type == "missing"
    )
  )


gt_line_names = b$`_data`$label

for (j in seq_along(gt_line_names)){
  if (startsWith(gt_line_names[j], "ENV")){
    b = b |> 
      tab_stub_indent(
        rows = j,
        indent = 5
      )
  }
}
b|> gtsave("Population.docx")

I would expect this output (produced in the html file) with labels correctly indented Correct HTML output

But I end up with this : Problematic docx output


Solution

  • Contrary to the vignette about compatibility of exports, I'm concerned that maybe the compatibility isn't fully achieved yet

    In the gt package (the package that prints the tables), Word does not support every modification/styling available for HTML output. The gtsummary package takes advantage of styling that is supported in docx format, and you've added styling that is not yet supported. (There are MANY many items that are supported in HTML and not docx.)

    You have a couple of options:

    1. The easiest thing to do would to keep the default styling from the gtsummary package.

    2. You can also explore exporting to flextable with as_flex_table() , which also has loads of ways to style a table and has great Word support.

    3. You can file an issue with the gt package requesting support for the specific styling you wish to see in the Word output.