This repository has been archived by the owner on Sep 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app-multi-panel.R
280 lines (224 loc) · 9.82 KB
/
app-multi-panel.R
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
library(shiny)
library(tidyverse)
library(httr)
library(jsonlite)
library(DT)
library(googleAuthR)
# Configure the OAuth request
# GCP API scope
scopes="https://www.googleapis.com/auth/cloud-platform"
# This is a test web application OAuth registration, requiring special access
# Setup here: https://console.developers.google.com/apis/credentials
gar_set_client(web_json = "AnVIL FHIR Test Web.json",
scopes = scopes,
activate="web")
ui <- fluidPage(
# Application title
titlePanel("NCPI Study Summary FHIR Browser"),
# Main page that sets up a workflow of tabs
navbarPage("=>",
tabPanel("ResearchStudy Browser",
fluidPage(
fluidRow(
column(4,
helpText("Select a study:"),
DTOutput("study_table")
),
column(8,
fluidPage(
fluidRow(htmlOutput("study_detail_header")),
fluidRow(column(6,plotOutput("study_detail_race")),
column(6,plotOutput("study_detail_eth"))),
fluidRow(column(4,tableOutput("study_detail_race_eth_tab")),
column(2,tableOutput("study_detail_gender_tab")),
column(6,plotOutput("study_detail_gender")))
)
)
)
)
),
tabPanel("Study Phenotypes Browser",fluidPage(
fluidRow(
column(4,
htmlOutput("study_detail_header_part"),
helpText("List of groups in this study:"),
DTOutput("study_group_table")
),
column(8,
fluidPage(
helpText("Phenotype Summary"),
DTOutput("participant_phenotype_summary")
)
)
)
)
),
tabPanel("Configuration",
helpText("Please indicate the FHIR base URL"),
textInput("server",
"Server Loc:",
value = "https://healthcare.googleapis.com/v1/projects/anvil-fhir-vumc/locations/us-central1/datasets/anvil-public-test/fhirStores/anvil-public-2021Q4/fhir/")
)
)
)
server <- function(input, output, session) {
# create a non-reactive access_token as we should never get past this if not authenticated
gar_shiny_auth(session)
## Load helper functions
source("support_functions.R", local=TRUE)
## Create some reactive expressions for each step so the plots don't cause too many calls
###
#Block for ResearchStudy Tab
###
# Get the list of studies
studies <- reactive({
req(input$server)
get_all("ResearchStudy")
})
# Study group lookup
groups_by_study_id <- reactive({
tibble(studies=studies()) %>% unnest_wider(studies) %>%
unnest_longer(enrollment) %>% unnest_wider(enrollment) %>%
transmute(study_id=id, group_reference=reference, group_id=substring(reference,7))
})
# Get all groups referenced
all_groups <- reactive({
get_resource_list(groups_by_study_id()[["group_reference"]])
})
# Parse groups
groups <- reactive({
parse_groups(all_groups())
})
# Complete groups
complete_group <- reactive({
groups() %>% filter(complete_group)
})
#Get demographics
complete_group_demographics <- reactive({
bind_rows(extract_stats(get_all(paste("Observation?code:text=Gender%20Variable%20Summary&subject=",
paste0("Group/",complete_group()[["group_id"]],collapse = ","),sep = ""))),
extract_stats(get_all(paste("Observation?code:text=Race%20Variable%20Summary&subject=",
paste0("Group/",complete_group()[["group_id"]],collapse = ","),sep = ""))),
extract_stats(get_all(paste("Observation?code:text=69490-1%20Variable%20Summary&subject=",
paste0("Group/",complete_group()[["group_id"]],collapse = ","),sep = ""))))
})
# Summarize in a table
studyTable <- reactive({
bind_rows(lapply(studies(), function(x){
data.frame(study_id=x$id,
study_title=x$title)
})) %>%
inner_join(groups_by_study_id()) %>%
inner_join(complete_group()) %>%
select(study_id, study_title, n_participants) %>%
arrange(study_title)
})
# Create output
output$study_table <- renderDT(
datatable(studyTable() %>% transmute(study_id, `Study Title`=study_title, `Participants`=n_participants),
selection = "single", rownames=F,
options=list(columnDefs = list(list(visible=FALSE, targets=c(0)))))
)
###
#Block for research study detail tab
###
selected_study_row <- reactive({
req(input$study_table_rows_selected)
studyTable()[input$study_table_rows_selected,]
})
selected_study_id <- reactive({
selected_study_row()[["study_id"]]
})
selected_study_groups <- reactive({
req(input$study_table_rows_selected)
groups_by_study_id() %>% filter(study_id==selected_study_id())
})
#Get details for that ID
study_groups <- reactive({
groups() %>% inner_join(selected_study_groups())
})
studyGroupTable <- reactive({
study_groups() %>%
arrange(n_participants) %>%
transmute(group_id, `Group Name`=group_name, `Participants`=n_participants)
})
# Create output
output$study_group_table <- renderDT(
datatable(studyGroupTable(),selection = "single", rownames=F,
options=list(columnDefs = list(list(visible=FALSE, targets=c(0)))))
)
selected_group_id <- reactive({
req(input$study_group_table_rows_selected)
studyGroupTable()[input$study_group_table_rows_selected,][["group_id"]]
})
##Study Summary tabs
# Create the study summary
output$study_detail_header_part <- output$study_detail_header <- renderText({
sprintf("<h2>%s</h2><br/>Study ID: %s",
studyTable()[input$study_table_rows_selected,"study_title"],
studyTable()[input$study_table_rows_selected,"study_id"])
})
studyDemographicTable <- reactive({
complete_group_demographics() %>%
inner_join(selected_study_groups())
})
output$study_detail_gender <- renderPlot({
gender_table = studyDemographicTable() %>%
filter(term_name=="Gender", !grepl("^Total",component_text))
ggplot(gender_table, aes(x="",y=component_value,fill=component_text)) +
geom_bar(stat="identity", width=1) +
coord_polar("y", start=0) +
theme_void() +
ggtitle("Gender")
})
output$study_detail_race <- renderPlot({
race_table = studyDemographicTable() %>%
filter(term_name=="Race", !grepl("^Total",component_text)) %>%
mutate(component_text=str_replace(component_text,"Demographics .*:",""))
ggplot(race_table,
aes(x=component_text, y=component_value,fill=component_text)) +
geom_bar(stat="identity") +
ggtitle("Race") +
theme_light() +
theme(legend.position="bottom",
axis.text.x = element_text(angle = -15))
})
output$study_detail_eth <- renderPlot({
eth_table = studyDemographicTable() %>%
filter(term_name=="Ethnicity", !grepl("^Total",component_text)) %>%
mutate(component_text=str_replace(component_text,"Demographics .*:",""))
ggplot(eth_table,
aes(x=component_text, y=component_value,fill=component_text)) +
geom_bar(stat="identity") +
theme_light() +
ggtitle("Ethnicity") +
theme(legend.position="bottom",
axis.text.x = element_text(angle = -15))
})
output$study_detail_gender_tab <- renderTable({
studyDemographicTable() %>%
filter(term_name=="Gender", !grepl("^Total",component_text)) %>%
transmute(`Category`=component_text, `Participants`=component_value)
})
output$study_detail_race_eth_tab <- renderTable({
studyDemographicTable() %>%
filter(term_name=="Race", !grepl("^Total",component_text)) %>%
transmute(`Category`=component_text, `Participants`=component_value)
})
##Participant Summary tabs
# Create the participant summary
phenotypeTable <- reactive({
extract_stats(get_all(paste("Observation?code:text=Variable%20Summary&subject=Group/",
selected_group_id(),sep = ""))) %>%
filter(grepl("Phenotype",component_text)) %>%
mutate(term_name=str_replace(term_name,"Present: ","")) %>%
pivot_wider(id_cols=term_name,names_from=component_text,values_from=component_value,
values_fill=0, values_fn=max) %>%
rename(Phenotype=term_name) %>% arrange(-`Phenotype Present`)
})
output$participant_phenotype_summary <- renderDT({
datatable(phenotypeTable(),rownames=F)
})
}
# Run the application
shinyApp(ui = gar_shiny_ui(ui), server = server, options = list(port=1221))