I have created a gtsummary table using the theme_gtsummary_compact() and would like to show a superscript in one of my variable labels. This last part works with the fmt_markdown() function but I then loose the compact table design.
Here is example code:
library(dplyr)
library(gtsummary)
library(gt)
# Set seed for reproducibility
set.seed(123)
# Create data set
mock_data <- tibble(
age = sample(18:90, 100, replace = TRUE),
ethnicity = sample (c("White","Black","Asian","Hispanic","Other"), 100, replace = TRUE),
bmi = round(runif(100, 18, 40), 1),
severity = sample(c("Mild","Moderate","Severe"), 100, replace = TRUE)
)
# Create gtsummary table
theme_gtsummary_compact()
summary_table <- mock_data %>%
tbl_summary(
by = severity,
label = list(
age ~ "Age (in years)",
ethnicity ~ "Ethnicity",
bmi ~ "BMI (kg/m<sup>2</sup>)",
severity ~ "Disease Severity"
)
) %>%
add_p()
summary_table
# Convert to gt table and format HTML labels
gt_table <- summary_table %>%
as_gt() %>%
fmt_markdown(columns = c(label))
gt_table
When I add (after the fmt_markdown) tab_options for fonts, row.padding etc, I get a partially compacted table (for the numeric variables, but not for the individual categories of any categorical variable). I also loose other attributes of the theme, for example the indentations for the individual character levels. What is the best solution to get the table to resemble the original compact table but with the superscript in the label?
gt_table <- summary_table %>%
as_gt() %>%
fmt_markdown(columns = c(label)) %>%
tab_options(
table.font.size = "small",
heading.title.font.size = "small",
heading.subtitle.font.size = "small",
table.border.top.width = px(0),
table.border.bottom.width = px(0),
column_labels.border.top.width = px(0),
column_labels.border.bottom.width = px(0),
data_row.padding = px(1),
summary_row.padding = px(1)
)
gt_table
And lastly, how would we export this table successfully to a Word document while retaining any and all formats?