forked from mhira-project/data-export
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
259 lines (176 loc) · 7.22 KB
/
app.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
#Load packages ----
library(shiny)
library(shinydashboard)
library(tidyverse)
library(RColorBrewer)
library(data.table)
library(httr)
library(jsonlite)
library(DT)
library(crosstalk)
# APP SETTINGS ----------------------------------------------------------------
if(!file.exists("settings.R")){
source("settings-default.R")} else {
source("settings.R")} # To customise settings, please create settings.R
# LOAD GRAPHQL ----------------------------------------------------------------
source("graphql_functions/getPatientIds.R")
source("graphql_functions/getMultiplePatientReports.R")
# LOAD UTILITY ----------------------------------------------------------------
source("utility_functions/simplifyPatRep.R")
source("utility_functions/simplifyMultPatRep.R")
source("utility_functions/calculateScales.R")
source("utility_functions/applyCutOffs.R")
source("utility_functions/severityPlot.R")
source("utility_functions/inactivity.R")
source("utility_functions/interpretTable.R")
source("utility_functions/checkGraphqlResponse.R")
source("utility_functions/patientInfoTable.R")
source("utility_functions/extract_cutoffs.R")
source("utility_functions/groupCutoffs.R")
source("utility_functions/printItemTable.R")
inactivity = inactivity(timeoutSeconds)
# LOAD TRANSLATION MATRIX -----------------------------------------------------
transMatrix = data.frame(fread("www/transMatrix.csv"), row.names = "Key")
# USER INTERFACE ##------------------------------------------------------------
ui <- dashboardPage(skin = "purple",
title="MHIRA",
# HEADER ------------------------------------------------------------------
dashboardHeader(
title = tags$a(href='http://mhira-project.org',
tags$img(src='mhira_logo.png', height='50', width='150'),
'MHIRA')
),
# SIDEBAR ------------------------------------------------------------------
dashboardSidebar(
width = 250,
collapsed = FALSE,
tags$script(inactivity), # For timeout
tags$script(HTML( # This javascript code gets data from localStorage of the browser
"$(document).on('shiny:connected', function() {
const LS = window.localStorage.getItem('auth_app_token');
Shiny.setInputValue('accessToken', LS);
const CL = window.localStorage.getItem('currentLang');
Shiny.setInputValue('currentLang', CL);
});"
)),
br(),
uiOutput("selectAss")
),
# BODY -------------------------------------------------------------------
dashboardBody(
includeCSS("www/myCSS.css"),
fluidRow(
h1("Data export"),
br(),
downloadButton("downloadData", "Download Data"),
br(),
br(),
tags$p("Downloaded file will be a CSV with semicolon used to separate colums."),
tags$p("Loading the data can take a moment... In case data is not ready, a html file will be downloaded instead of the CSV. In this case, try again after a moment.")
),
fluidRow(
br(),
br()
)
)
# CLOSE USER INTERFACE UI ---------------------------------------------------
)
## SERVER ## -----------------------------------------------------------------
server = function(input, output, session) {
# OBSERVE INACTIVITY AND CLOSE APP ------------------------------------------
observeEvent(input$timeOut, {
print(paste0("Session was closed at", " ", Sys.time()))
showModal(modalDialog(
title = "Timeout",
paste("Session was closed afer",
input$timeOut
),
footer = NULL
))
session$close()
})
# STORE LOCAL STORAGE EXTRACTED TOKEN TO SHINY SESSION OBJECT ----------------
observe({
print("writing token to session object")
session$userData = fromJSON(input$accessToken)$accessToken
}) %>% bindEvent(input$accessToken)
# GET PATIENT IDS FROM MHIRA
patientIds = reactiveVal()
observe({
req(!is_empty(session$userData))
print("getting patient IDs from MHIRA")
Patients = getPatientIds(token = session$userData, url = url)
if (length(Patients) > 0){
patientIds(Patients$id)
} else {
showNotification(
"No patients found",
type = "error",
duration = 20)
session$close()
}
}) %>% bindEvent(input$accessToken)
# GET PATIENT REPORT DATA ---------------------------------------------------
response = reactiveVal() # the imported data as dataframe
observe({
req(!is_empty(patientIds()))
req(!is_empty(session$userData))
print("get patient report via graphql")
response = getMultiplePatientReports(token = session$userData, patientIds = patientIds(), url = url)
# checkGraphqlResponse(response, session) # can close session
response(response)
print("data has been obtained from API")
}) %>% bindEvent(patientIds())
# CALCULATE SCALES AND APPLY CUTOFFS -----------------------------------------
data = reactiveVal() # the imported data as dataframe
scales = reactiveVal() # calculated scales
cutoffs = reactiveVal() # only cutoffs without data
observe({
req(!is_empty(response()))
response = response()
# Simplify data and remove incomplete questionnaires
data = simplifyMultPatRep(response = response)
# Terminate session if no completed data
dataNotOkay = FALSE
if(is_empty(data)){
dataNotOkay = TRUE
} else {
if(data %>% nrow < 1){dataNotOkay = TRUE}
}
if(dataNotOkay){
showNotification(
"Problem with data. Does the user have access to patient data?",
type = "error",
duration = 20)
session$close()
}
# Extract data from scripts
# questionnaireScripts = response$data$generatePatientReport$questionnaireScripts
#
# cutoffs = extract_cutoffs(questionnaireScripts = questionnaireScripts)
# cutoffs = groupCutoffs(cutoffs = cutoffs)
#
# scales = calculateScales(
# simplifiedData = data,
# questionnaireScripts = questionnaireScripts)
#
#
# scales = applyCutOffs(scales = scales, cutoffs = cutoffs)
data(data)
output$data = data %>% renderDT()
dataForDownload = data %>% select(-questionnaireScripts, -choices, -multipleChoiceValue) %>% as.data.frame
output$downloadData <- downloadHandler(
filename = function() {
paste("data_", format(Sys.time(), "%Y-%m-%d_%H-%M-%S"), ".csv", sep = "")
},
content = function(file) {
fwrite(dataForDownload, file, sep = ";")
}
)
# scales(scales)
# cutoffs(cutoffs)
print("scales have been calculated and cutoffs applied")
}) %>% bindEvent(response())
}
## APP ## --------------------------------------------------------------------
shinyApp(ui = ui, server = server)