最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - gtsave() to docx loses tab_stub_indent in the process - Stack Overflow

programmeradmin1浏览0评论

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 .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

But I end up with this :

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/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

But I end up with this :

Share Improve this question asked Mar 11 at 15:02 SigilSigil 256 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

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.

发布评论

评论列表(0)

  1. 暂无评论