-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtfrmt.R
More file actions
188 lines (173 loc) · 7.28 KB
/
tfrmt.R
File metadata and controls
188 lines (173 loc) · 7.28 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
pacman::p_load(tfrmt, dplyr, gt, tidyr)
data_demog
data_demog %>%
distinct(rowlbl1,grp)
tfrmt(
# specify columns in the data
group = c(rowlbl1,grp),
label = rowlbl2,
column = column,
param = param,
value = value,
sorting_cols = c(ord1, ord2)) %>%
print_to_gt(data_demog) %>%
tab_options(
container.width = 900
)
tfrmt(
# specify columns in the data
group = c("rowlbl1", "grp"),
label = "rowlbl2",
column = "column",
param = "param",
value = "value",
sorting_cols = c("ord1", "ord2")) %>%
print_to_gt(data_demog) %>%
tab_options(
container.width = 900
)
tfrmt(
# specify columns in the data
group = c("rowlbl1","grp"),
label = "rowlbl2",
column = "column",
param = "param",
value = "value",
sorting_cols = c("ord1", "ord2"),
# specify value formatting
body_plan = body_plan(
frmt_structure(group_val = ".default", label_val = ".default", frmt_combine("{n} ({pct} %)",
n = frmt("xxx"),
pct = frmt("xx.x"))),
frmt_structure(group_val = ".default", label_val = "n", frmt("xxx")),
frmt_structure(group_val = ".default", label_val = c("Mean", "Median", "Min","Max"), frmt("xxx.x")),
frmt_structure(group_val = ".default", label_val = "SD", frmt("xxx.xx")),
frmt_structure(group_val = ".default", label_val = ".default", p = frmt_when(">0.99" ~ ">0.99",
"<0.001" ~ "<0.001",
TRUE ~ frmt("x.xxx", missing = "")))
)) %>%
print_to_gt(data_demog) %>%
tab_options(
container.width = 900
)
tfrmt(
# specify columns in the data
group = c("rowlbl1","grp"),
label = "rowlbl2",
column = "column",
param = "param",
value = "value",
sorting_cols = c("ord1", "ord2"),
# specify value formatting
body_plan = body_plan(
frmt_structure(group_val = ".default", label_val = ".default", frmt_combine("{n} {pct}",
n = frmt("xxx"),
pct = frmt_when("==100" ~ "",
"==0" ~ "",
TRUE ~ frmt("(xx.x %)")))),
frmt_structure(group_val = ".default", label_val = "n", frmt("xxx")),
frmt_structure(group_val = ".default", label_val = c("Mean", "Median", "Min","Max"), frmt("xxx.x")),
frmt_structure(group_val = ".default", label_val = "SD", frmt("xxx.xx")),
frmt_structure(group_val = ".default", label_val = ".default", p = frmt("")),
frmt_structure(group_val = ".default", label_val = c("n","<65 yrs","<12 months","<25"), p = frmt_when(">0.99" ~ ">0.99",
"<0.001" ~ "<0.001",
TRUE ~ frmt("x.xxx", missing = "")))
),
# remove extra cols
col_plan = col_plan(-grp,
-starts_with("ord") )) %>%
print_to_gt(data_demog) %>%
tab_options(
container.width = 900
)
demotab <- tfrmt(
# specify columns in the data
group = c("rowlbl1","grp"),
label = "rowlbl2",
column = "column",
param = "param",
value = "value",
sorting_cols = c("ord1", "ord2"),
# specify value formatting
body_plan = body_plan(
frmt_structure(group_val = ".default", label_val = ".default", frmt_combine("{n} {pct}",
n = frmt("xxx"),
pct = frmt_when("==100" ~ "",
"==0" ~ "",
TRUE ~ frmt("(xx.x %)")))),
frmt_structure(group_val = ".default", label_val = "n", frmt("xxx")),
frmt_structure(group_val = ".default", label_val = c("Mean", "Median", "Min","Max"), frmt("xxx.x")),
frmt_structure(group_val = ".default", label_val = "SD", frmt("xxx.xx")),
frmt_structure(group_val = ".default", label_val = ".default", p = frmt("")),
frmt_structure(group_val = ".default", label_val = c("n","<65 yrs","<12 months","<25"), p = frmt_when(">0.99" ~ ">0.99",
"<0.001" ~ "<0.001",
TRUE ~ frmt("x.xxx", missing = "")))
),
# remove extra cols
col_plan = col_plan(-grp,
-starts_with("ord") ),
# Specify column styling plan
col_style_plan = col_style_plan(
col_style_structure(align = c(".",","," "), col = c("Placebo", "Xanomeline Low Dose",
"Xanomeline High Dose", "Total", "p-value")),
col_style_structure(align = "left", col = c("rowlbl1","rowlbl2"))
),
# Specify row group plan
row_grp_plan = row_grp_plan(
row_grp_structure(group_val = ".default", element_block(post_space = " ")),
label_loc = element_row_grp_loc(location = "column")
)
) %>%
print_to_gt(data_demog) %>%
tab_options(
container.width = 900
)
demotab |> gtsave("tab_1.docx")
##Adverse Effect ---------------------------------------------------------------
data_ae2 <- data_ae %>%
group_by(AEBODSYS, AETERM) %>%
mutate(pct_high = value[col2=="Xanomeline High Dose" & param=="pct"]) %>%
ungroup %>%
filter(pct_high >10) %>%
select(-pct_high)
tfrmt(
# specify columns in the data
group = "AEBODSYS",
label = "AETERM",
column = c("col2", "col1"),
param = "param",
value = "value",
sorting_cols = c("ord1", "ord2")) %>%
print_to_gt(data_ae2) %>%
tab_options(
container.width = 1000
)
tfrmt(
# specify columns in the data
group = "AEBODSYS",
label = "AETERM",
column = c("col2", "col1"),
param = "param",
value = "value",
sorting_cols = c("ord1", "ord2"),
# specify value formatting
body_plan = body_plan(
frmt_structure(group_val = ".default", label_val = ".default",
frmt_combine("{n} {pct}",
n = frmt("XXX"),
pct = frmt_when(
"==100" ~ "",
"==0" ~ "",
TRUE ~ frmt("(xx.x %)")))),
frmt_structure(group_val = ".default", label_val = ".default",
AEs = frmt("[XXX]")),
frmt_structure(group_val = ".default", label_val = ".default",
pval = frmt_when(">0.99" ~ ">0.99",
"<0.001" ~ "<0.001",
"<0.05" ~ frmt("x.xxx*"),
TRUE ~ frmt("x.xxx", missing ="--")))
)) %>%
print_to_gt(., data_ae2) %>%
tab_options(
container.width = 1000
)