server.R 15.1 KB
Newer Older
1
2
# DQAgui - A graphical user interface (GUI) to the functions implemented in the
# R package 'DQAstats'.
Jonathan Mang's avatar
Jonathan Mang committed
3
# Copyright (C) 2019-2020 Universitätsklinikum Erlangen
Lorenz Kapsner's avatar
Lorenz Kapsner committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

18
19
shiny::shinyServer(
    function(input, output, session) {
20
        invisible(gc())
21
        # define reactive values here
22
23
24
        rv <- shiny::reactiveValues(
            headless = FALSE,
            mdr_filename = mdr_filename,
Jonathan Mang's avatar
Jonathan Mang committed
25
26
            log = list(logfile_dir = DIZutils::clean_path_name(logfile_dir)),
            utilspath = DIZutils::clean_path_name(utils_path),
27
28
29
            current_date = format(Sys.Date(), "%d. %B %Y", tz = "CET"),
            parallel = parallel,
            ncores = ncores
30
        )
31

32
33
        shiny::observe({
            if (is.null(rv$finished_onstart)) {
34

35
                # Clean old connections (e.g. after reloading the app):
Jonathan Mang's avatar
Jonathan Mang committed
36
                DIZutils::close_all_connections(
37
38
39
                    logfile_dir = rv$log$logfile_dir,
                    headless = rv$headless
                )
40

41
                # Create new logfile:
42
                DIZutils::cleanup_old_logfile(
43
44
45
46
                    logfile_dir = rv$log$logfile_dir
                )

                # feedback directories
Jonathan Mang's avatar
Jonathan Mang committed
47
                DIZutils::feedback(
48
49
50
51
52
53
54
                    print_this = paste0(
                        "Logfile dir: ",
                        rv$log$logfile_dir
                    ),
                    logfile_dir = rv$log$logfile_dir,
                    headless = rv$headless
                )
Jonathan Mang's avatar
Jonathan Mang committed
55
                DIZutils::feedback(
56
57
58
59
60
61
62
                    print_this = paste0(
                        "Utils path: ",
                        rv$utilspath
                    ),
                    logfile_dir = rv$log$logfile_dir,
                    headless = rv$headless
                )
Jonathan Mang's avatar
Jonathan Mang committed
63
                DIZutils::feedback(
64
65
66
67
68
69
70
71
72
73
74
75
                    print_this = paste0(
                        "MDR filename: ",
                        rv$mdr_filename
                    ),
                    logfile_dir = rv$log$logfile_dir,
                    headless = rv$headless
                )

                # read datamap email
                rv$datamap_email <- tryCatch(
                    expr = {
                        # if existing, set email address for data-map button
Jonathan Mang's avatar
Jonathan Mang committed
76
                        out <- DIZutils::get_config(
77
78
79
80
81
82
83
84
85
86
87
88
89
90
                            config_file = paste0(
                                utils_path, "/MISC/email.yml"
                            ),
                            config_key = "email",
                            logfile_dir = rv$log$logfile_dir,
                            headless = rv$headless
                        )
                    }, error = function(e) {
                        print(e)
                        # otherwise set it to empty string
                        out <- ""
                    }, finally = function(f) {
                        return(out)
                    })
91
92
93
94
95
96
97
98


                DQAstats::parallel(
                    parallel = rv$parallel,
                    logfile_dir = rv$log$logfile_dir,
                    ncores = rv$ncores
                )

99
100
101
                rv$finished_onstart <- TRUE
            }
        })
102
103
104

        # handle reset
        shiny::observeEvent(input$reset, {
Jonathan Mang's avatar
Jonathan Mang committed
105
            DIZutils::feedback(
106
107
108
109
                print_this = "\U2304",
                logfile_dir = rv$log$logfile_dir,
                headless = rv$headless
            )
Jonathan Mang's avatar
Jonathan Mang committed
110
            DIZutils::feedback(
111
                print_this = "############ APP WAS RESETTED ############",
112
113
114
115
                findme = "9c57ce125a",
                logfile_dir = rv$log$logfile_dir,
                headless = rv$headless
            )
Jonathan Mang's avatar
Jonathan Mang committed
116
            DIZutils::feedback(
117
118
119
120
                print_this = "\U2303",
                logfile_dir = rv$log$logfile_dir,
                headless = rv$headless
            )
Jonathan Mang's avatar
Jonathan Mang committed
121
            DIZutils::close_all_connections(
122
123
                logfile_dir = rv$log$logfile_dir,
                headless = rv$headless
124
            )
125
126
127
            ## Old (until 2021-03-15):
            # shinyjs::js$reset()
            ## New (since 2021-03-15):
128
            rm(list = ls())
129
130
131
132
            rm(rv)
            # invisible(gc())
            gc()
            gc()
Jonathan Mang's avatar
Jonathan Mang committed
133
            gc(reset = TRUE)
134
            session$reload()
135
        })
136

137
138
139
        input_reactive <- reactive({
            input
        })
140

141
142
143
        # ########################
        # # tab_config
        # ########################
kapsner's avatar
kapsner committed
144

145
146
147
148
        shiny::callModule(module_config_server,
                          "moduleConfig",
                          rv,
                          input_re = input_reactive)
kapsner's avatar
kapsner committed
149

150
151
152
153
154
155
156
        shiny::observe({
            # first call (rv$target_getdata = TRUE and
            # rv$source_getdata = TRUE),
            # when load-data-button quality checks in
            # moduleDashboard are passed
            if (!is.null(rv$getdata_target) &&
                !is.null(rv$getdata_source)) {
157

158
159
160
161
162
163
164
165
                if (!isTRUE(rv$start)) {
                    # set active tab
                    shinydashboard::updateTabItems(
                        session = session,
                        inputId = "tabs",
                        selected = "tab_dashboard"
                    )
                }
166

167
168
                # hide load data button
                shinyjs::hide("moduleConfig-dash_load_btn")
kapsner's avatar
kapsner committed
169

170
171
172
173
174
175
176
177
178
179
180
                shinyjs::disable("moduleConfig-config_sourcedir_in")
                shinyjs::disable("moduleConfig-source_csv_presettings_list")
                shinyjs::disable("moduleConfig-source_csv_dir")
                shinyjs::disable("moduleConfig-source_tabs")
                shinyjs::disable("moduleConfig-source_pg_presettings_list")
                shinyjs::disable("moduleConfig-config_sourcedb_dbname")
                shinyjs::disable("moduleConfig-config_sourcedb_host")
                shinyjs::disable("moduleConfig-config_sourcedb_port")
                shinyjs::disable("moduleConfig-config_sourcedb_user")
                shinyjs::disable("moduleConfig-config_sourcedb_password")
                shinyjs::disable("moduleConfig-source_pg_test_connection")
181

182
183
184
185
186
187
188
189
190
191
192
                shinyjs::disable("moduleConfig-config_targetdir_in")
                shinyjs::disable("moduleConfig-target_csv_presettings_list")
                shinyjs::disable("moduleConfig-target_csv_dir")
                shinyjs::disable("moduleConfig-target_tabs")
                shinyjs::disable("moduleConfig-target_pg_presettings_list")
                shinyjs::disable("moduleConfig-config_targetdb_dbname")
                shinyjs::disable("moduleConfig-config_targetdb_host")
                shinyjs::disable("moduleConfig-config_targetdb_port")
                shinyjs::disable("moduleConfig-config_targetdb_user")
                shinyjs::disable("moduleConfig-config_targetdb_password")
                shinyjs::disable("moduleConfig-target_pg_test_connection")
Jonathan Mang's avatar
Jonathan Mang committed
193
194
195
                shinyjs::disable("moduleConfig-select_dqa_assessment_variables")
                shinyjs::disable("moduleConfig-select_all_assessment_variables")
                shinyjs::disable("moduleConfig-select_no_assessment_variables")
196
197
198
199
200

                shinyjs::disable("moduleConfig-config_sitename")
                shinyjs::disable(
                    "moduleConfig-target_system_to_source_system_btn"
                )
201

202
203
                shinyjs::disable(id = "moduleConfig-datetime_picker")

204
205
206
                if (input$tabs == "tab_dashboard") {
                    rv$start <- TRUE
                }
207
            }
208
        })
kapsner's avatar
kapsner committed
209

210
211
212
        shiny::observe({
            req(rv$mdr)
            shinyjs::disable("moduleConfig-config_load_mdr")
213

214
215
216
217
218
219
220
221
222
223
224
225
            output$mdr <- shinydashboard::renderMenu({
                shinydashboard::sidebarMenu(shinydashboard::menuItem(
                    "DQ MDR",
                    tabName = "tab_mdr",
                    icon = icon("database")
                ))
            })
            shinydashboard::updateTabItems(
                session = session,
                inputId = "tabs",
                selected = "tab_config"
            )
kapsner's avatar
kapsner committed
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
        shiny::observe({
            shiny::req(rv$report_created)

            # set end_time
            rv$end_time <- format(Sys.time(), usetz = T, tz = "CET")
            # calc time-diff
            rv$duration <-
                difftime(rv$end_time, rv$start_time, units = "mins")

            # render menu
            output$menu <- shinydashboard::renderMenu({
                shinydashboard::sidebarMenu(
                    #shinydashboard::menuItem("Review raw data",
                    #tabName = "tab_rawdata1", icon = icon("table")),
                    shinydashboard::menuItem(
                        text = "Descriptive Results",
                        tabName = "tab_descriptive",
                        icon = icon("table")
                    ),
                    shinydashboard::menuItem(
                        text = "Plausibility Checks",
                        tabName = "tab_plausibility",
                        icon = icon("check-circle"),
                        shinydashboard::menuSubItem(
                            text = "Atemporal Plausibility",
                            tabName = "tab_atemp_plausibility"
                        ),
                        shinydashboard::menuSubItem(
                            text = "Uniqueness Plausibility",
                            tabName = "tab_uniq_plausibility"
                        )
259
                    ),
260
261
262
263
264
265
266
267
268
269
270
271
                    shinydashboard::menuItem(
                        text = "Completeness",
                        tabName = "tab_completeness",
                        icon = icon("chart-line")
                    ),
                    #shinydashboard::menuItem("Visualizations",
                    #tabName = "tab_visualizations",
                    #icon = icon("chart-line")),
                    shinydashboard::menuItem(
                        text = "Reporting",
                        tabName = "tab_report",
                        icon = icon("file-alt")
272
273
                    )
                )
274
275
276
277
278
            })
            shinydashboard::updateTabItems(
                session = session,
                inputId = "tabs",
                selected = "tab_dashboard"
kapsner's avatar
kapsner committed
279
280
281
            )
        })

282

283
        shiny::observe({
284
            req(rv$datamap)
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299

            # !!! trigger shinyjs from server.R only
            shinyjs::onclick(
                # https://stackoverflow.com/questions/27650331/adding-an-email-
                # button-in-shiny-using-tabletools-or-otherwise
                # https://stackoverflow.com/questions/27650331/adding-an-email-
                # button-in-shiny-using-tabletools-or-otherwise
                # https://stackoverflow.com/questions/37795760/r-shiny-add-
                # weblink-to-actionbutton
                # https://stackoverflow.com/questions/45880437/r-shiny-use-
                # onclick-option-of-actionbutton-on-the-server-side
                # https://stackoverflow.com/questions/45376976/use-
                # actionbutton-to-send-email-in-rshiny
                id = "moduleDashboard-dash_send_datamap_btn",
                expr =  {
300
                    rv$send_datamap <- button_send_datamap(rv)
301
302
303
304
                    shinyjs::runjs(rv$send_datamap)
                }
            )
        })
305

306
        shiny::observe({
307
308
309
310
            req(rv$datamap)
            # To allow only one export, disable button afterwards:
            if (is.null(rv$send_btn_disabled)) {
                if (isTRUE(rv$datamap$exported)) {
Lorenz Kapsner's avatar
Lorenz Kapsner committed
311
                    # so don't send it again
312
313
314
315
316
317
318
319
320
321
322
323
324
325
                    updateActionButton(
                        session = session,
                        inputId = "moduleDashboard-dash_send_datamap_btn",
                        label = "Datamap successfully sent",
                        icon = icon("check")
                    )
                    shinyjs::disable(
                        "moduleDashboard-dash_send_datamap_btn"
                    )
                    rv$send_btn_disabled <- TRUE
                }
            }
        })

326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
        ########################
        # tab_dashboard
        ########################
        shiny::callModule(module_dashboard_server,
                          "moduleDashboard",
                          rv,
                          input_re = input_reactive)

        ########################
        # tab_descriptive
        ########################
        shiny::callModule(module_descriptive_server,
                          "moduleDescriptive",
                          rv,
                          input_re = input_reactive)

        ########################
        # tab_plausibility
        ########################
        shiny::callModule(module_atemp_pl_server,
                          "moduleAtempPlausibility",
                          rv,
                          input_re = input_reactive)
        shiny::callModule(module_uniq_plaus_server,
                          "moduleUniquePlausibility",
                          rv,
                          input_re = input_reactive)

        ########################
        # tab_completeness
        ########################
        shiny::callModule(module_completeness_server,
                          "moduleCompleteness",
                          rv,
                          input_re = input_reactive)

        # ########################
        # # tab_visualization
        # ########################
        #% shiny::callModule(module_visualizations_server,
        #%                   "moduleVisulizations",
        #%                   rv,
        #%                   input_re = input_reactive)

        ########################
        # tab_report
        ########################
        shiny::callModule(module_report_server,
                          "moduleReport",
                          rv,
                          input_re = input_reactive)

        ########################
        # tab_mdr
        ########################
        shiny::callModule(module_mdr_server,
                          "moduleMDR",
                          rv,
                          input_re = input_reactive)

Jonathan Mang's avatar
Jonathan Mang committed
386
387
388
389
390
391
392
393
        ########################
        # tab_log
        ########################
        shiny::callModule(module_log_server,
                          "moduleLog",
                          rv,
                          input_re = input_reactive)

394
        shiny::observe({
395
396
397
398
399
400
401
402
403
            if (input$tabs == "tab_log") {
                updateSelectInput(
                    session = session,
                    inputId = "moduleLog-old_logfiles_list",
                    selected = "logfile.log"
                )
                shinyjs::click("moduleLog-moduleLog_scrolldown_btn")
            }
        })
404
    })