-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgt.R
More file actions
141 lines (133 loc) · 3.49 KB
/
gt.R
File metadata and controls
141 lines (133 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# Required packages
library(gt)
library(gtsummary)
library(dplyr)
library(tidyr)
# Example 1: gt package - Creating a custom formatted table
mtcars_gt <- mtcars %>%
head(5) %>%
gt() %>%
tab_header(
title = "Motor Trend Car Data",
subtitle = "Top 5 Cars"
) %>%
fmt_number(
columns = c(mpg, disp, wt),
decimals = 1
) %>%
tab_style(
style = list(
cell_fill(color = "lightblue")
),
locations = cells_column_labels()
)
# Example 2: gtsummary - Creating statistical summary tables
# Descriptive statistics
mtcars_summary <- mtcars %>%
select(mpg, cyl, wt) %>%
tbl_summary(
statistic = list(
all_continuous() ~ "{mean} ({sd})",
all_categorical() ~ "{n} ({p}%)"
),
digits = all_continuous() ~ 1
) %>%
add_n() %>%
modify_header(label = "**Variable**")
# Example 3: gtsummary - Regression model summary
model <- lm(mpg ~ wt + cyl, data = mtcars)
model_summary <- tbl_regression(model,
label = list(
wt ~ "Weight",
cyl ~ "Cylinders"
)
) %>%
add_glance_source_note()
# Example 4: gtsummary - Comparing groups
mtcars_compare <- mtcars %>%
mutate(vs = factor(vs, labels = c("V-shaped", "Straight"))) %>%
select(mpg, wt, vs) %>%
tbl_summary(
by = vs,
missing = "no"
) %>%
add_p() %>%
add_overall() %>%
add_n()
comparison_data <- data.frame(
Feature = c(
"Primary Purpose",
"Table Types",
"Learning Curve",
"Statistical Features",
"Customization Level",
"Data Input",
"Common Use Cases",
"Dependencies",
"Output Format",
"Formula Interface"
),
gt = c(
"General table creation and formatting",
"Any type of data table",
"Steeper - requires more code for basic tables",
"Basic, requires manual implementation",
"High - complete control over every aspect",
"Data frames, matrices, tibbles",
"Custom reports, data presentation, publication tables",
"Standalone package",
"HTML, RTF, PDF, Word",
"No built-in formula interface"
),
gtsummary = c(
"Statistical and clinical summary tables",
"Summary statistics, regression results, comparison tables",
"Gentler - pre-built functions for common tables",
"Rich statistical functions built-in",
"Medium - preset formats with some customization",
"Data frames with focus on statistical data",
"Clinical research, statistical reports, regression summaries",
"Built on top of gt package",
"Same as gt, optimized for statistical reporting",
"Supports formula interface for models"
)
)
comparison_table <- comparison_data %>%
gt() %>%
tab_header(
title = "Comparison of gt vs gtsummary Packages",
subtitle = "Feature-by-Feature Comparison"
) %>%
tab_style(
style = list(
cell_fill(color = "#f0f0f0"),
cell_text(weight = "bold")
),
locations = cells_column_labels()
) %>%
tab_style(
style = list(
cell_fill(color = "#e6e6ff"),
cell_text(weight = "bold")
),
locations = cells_column_labels(columns = gt)
) %>%
tab_style(
style = list(
cell_fill(color = "#ffe6e6"),
cell_text(weight = "bold")
),
locations = cells_column_labels(columns = gtsummary)
) %>%
cols_width(
Feature ~ px(200),
gt ~ px(300),
gtsummary ~ px(300)
) %>%
tab_style(
style = list(
cell_text(weight = "bold")
),
locations = cells_body(columns = Feature)
)
comparison_table