library(gtsummary)
library(dplyr)
set.seed(123)
<- tibble(
data id = 1:100,
age = sample(18:90, 100, replace = TRUE),
sex = sample(c("Male", "Female"), 100, replace = TRUE),
race = sample(c("White", "Black", "Asian", "Hispanic"), 100, replace = TRUE),
bmi = round(runif(100, 18, 35), 1),
smoking_status = sample(c("Never", "Former", "Current"), 100, replace = TRUE),
diabetes = sample(c("Yes", "No"), 100, replace = TRUE),
hypertension = sample(c("Yes", "No"), 100, replace = TRUE),
heart_disease = sample(c("Yes", "No"), 100, replace = TRUE),
cancer = sample(c("Yes", "No"), 100, replace = TRUE),
cholesterol = sample(c("Normal", "High"), 100, replace = TRUE),
exercise_frequency = sample(0:7, 100, replace = TRUE),
alcohol_intake = sample(c("None", "Low", "Moderate", "High"), 100, replace = TRUE),
family_history = sample(c("Yes", "No"), 100, replace = TRUE),
education = sample(c("High School", "College", "Graduate", "Post-Grad", "Middle School", "Elementary School", "Pre School", "Kindergarten"), 100, replace = TRUE),
marital_status = sample(c("Single", "Married", "Divorced"), 100, replace = TRUE),
employment_status = sample(c("Employed", "Unemployed", "Retired"), 100, replace = TRUE),
region = sample(c("North", "South", "East", "West"), 100, replace = TRUE),
health_insurance = sample(c("Yes", "No"), 100, replace = TRUE),
medication = sample(c("Yes", "No"), 100, replace = TRUE),
visits_last_year = sample(0:10, 100, replace = TRUE),
satisfaction = sample(1:5, 100, replace = TRUE)
)
# Create a wide summary table
<- data %>%
summary_table tbl_summary(
by = education,
statistic = list(all_continuous() ~ "{mean} ({sd})",
all_categorical() ~ "{n} ({p}%)"),
label = list(
~ "Age (years)",
age ~ "BMI",
bmi ~ "Smoking Status",
smoking_status ~ "Diabetes",
diabetes ~ "Hypertension",
hypertension ~ "Heart Disease",
heart_disease ~ "Cancer",
cancer ~ "Cholesterol",
cholesterol ~ "Exercise Frequency (days/week)",
exercise_frequency ~ "Alcohol Intake",
alcohol_intake ~ "Family History",
family_history ~ "Marital Status",
marital_status ~ "Employment Status",
employment_status ~ "Region",
region ~ "Health Insurance",
health_insurance ~ "On Medication",
medication ~ "Visits Last Year",
visits_last_year ~ "Health Satisfaction (1-5)"
satisfaction
)%>%
) add_n() %>%
modify_header(label = "**Characteristic**", estimate = "**Mean (SD) or N (%)**") %>%
modify_table_styling(columns = everything()) %>%
add_overall() # This adds an overall column
Table Too Wide
Testing output when the table is too long
gt_save PDF
::gtsave(as_gt(summary_table), filename = "outputs/pdf/too_wide_gt_pdf.pdf") gt
rmarkdown rendered PDF
summary_table
gt_save rendered word file
::gtsave(as_gt(summary_table), filename = "outputs/docx/too_wide_gt_word.docx") gt
Preview the Output ### quarto rendered word file
gt_table
rmarkdown rendered word file
gt_table