admin管理员组文章数量:1410725
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 badges1 Answer
Reset to default 0Contrary 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:
The easiest thing to do would to keep the default styling from the gtsummary package.
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.You can file an issue with the gt package requesting support for the specific styling you wish to see in the Word output.
本文标签: rgtsave() to docx loses tabstubindent in the processStack Overflow
版权声明:本文标题:r - gtsave() to docx loses tab_stub_indent in the process - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744785983a2625002.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论