-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathGT_Workflow.Rmd
186 lines (145 loc) · 4.22 KB
/
GT_Workflow.Rmd
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
---
title: "GT"
output: html_notebook
---
With GT, it is simple to produce nice-looking display tables.
Features include annotations, table element styling, and text transformations that serve to communicate the subject matter more clearly. Tables can have:
A Table Header (optional; with a title and possibly a subtitle)
A Stub and the Stub Head (optional; contains row labels, optionally within row groups having row group labels and possibly summary labels when a summary is present)
Column Labels (contains column labels, optionally under spanner column labels)
Table Body (contains columns and rows of cells)
Table Footer (optional; possibly with footnotes and source notes)
This first part is creating a tidy tibble to pass into GT using openFDA data.
```{r include=FALSE}
library(tidyverse)
library(openfda)
library(ggthemes)
library(gt)
get_adverse <- function(gender, brand_name) {
fda_query("/drug/event.json") %>%
fda_filter("patient.drug.openfda.brand_name", brand_name) %>%
fda_filter("patient.patientsex", gender) %>%
fda_count("patient.reaction.reactionmeddrapt.exact") %>%
fda_exec()
}
total_events<- function(brand_name){
fda_query("/drug/event.json") %>%
fda_filter("patient.drug.openfda.brand_name", brand_name) %>%
fda_count("patient.reaction.reactionmeddrapt.exact") %>%
fda_exec()
}
serious_events<- function(brand_name){
fda_query("/drug/event.json") %>%
fda_filter("patient.drug.openfda.brand_name", brand_name) %>%
fda_count("serious") %>%
fda_exec()
}
drug<- "Keytruda"
female<-get_adverse(2, drug) %>% mutate(sex = 'female')
male<-get_adverse(1, drug)%>% mutate(sex = 'male')
all<- total_events(drug)
total_count<- sum(all$count)
serious<-serious_events(drug) %>% filter(term==1) %>% pull(count)
```
Here is a Raw gt Table
```{r}
gt_tbl <- gt(all[1:5,])
# Show the gt Table
gt_tbl
```
Now we will take out table and add the title and subtitle.
```{r}
gt_tbl <-
gt_tbl %>%
tab_header(
title = "Adverse Events",
subtitle = "The top five most reported events"
)
# Show the gt Table
gt_tbl
```
Now we are going to bold the title and subtitle.
```{r}
gt_tbl <-
gt_tbl %>%
tab_header(
title = md("**Adverse Events**"),
subtitle = md("The top ten most reported events")
)
gt_tbl
```
Now we are going to add some source notes.
```{r}
gt_tbl <-
gt_tbl %>%
tab_source_note(
source_note = "Source: https://open.fda.gov/"
) %>%
tab_source_note(
source_note = md("Reference: https://open.fda.gov/about/")
)
# Show the gt Table
gt_tbl
```
Now we are going to add a footnote only to rows 3 and 4.
```{r}
gt_tbl <-
gt_tbl %>%
tab_footnote(
footnote = "3rd and 4th place",
locations = cells_body(
columns = term,
rows = 3:4
)
)
# Show the gt Table
gt_tbl
```
Now we are going to add a footnote based on logic. Adding footnotes to the largest and smallest observations.
```{r}
largest <-
all[1:5,] %>%
arrange(desc(count)) %>%
slice(1) %>%
pull(term)
# Create two additional footnotes, using the
# `columns` and `where` arguments of `data_cells()`
gt_tbl <-
gt_tbl %>%
tab_footnote(
footnote = md("The **largest** by term."),
locations = cells_body(
columns = count,
rows = term == largest)
) %>%
tab_footnote(
footnote = "The lowest by population.",
locations = cells_body(
columns = count,
rows = count == min(count))
)
# Show the gt Table
gt_tbl
```
Here is a table with the parts combined and broken out by male and female.
### Events By Sex
```{r}
rbind(male,female) %>%
mutate(term=str_to_title(term)) %>%
pivot_wider(names_from=sex, values_from=count) %>%
mutate(Total=female+male) %>%
top_n(10, Total) %>%
gt(rowname_col="term") %>% tab_header(
title = md("Adverse Events"),
subtitle = "The top ten most reported events"
) %>% tab_source_note("All data queried from openFDA") %>%
tab_stubhead(label= 'Patient Reaction') %>%
tab_spanner(label = "By sex",
columns = c(female, male)) %>%
tab_spanner(label= 'Total Cases',
columns = Total) %>%
fmt_number(columns = c(female, male, Total), sep_mark = ',', decimals=0) %>%
cols_label(Total = '',
female = 'Female',
male= 'Male')
```