-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.R
More file actions
195 lines (165 loc) · 6.88 KB
/
app.R
File metadata and controls
195 lines (165 loc) · 6.88 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
189
190
191
192
193
194
195
# DeclareDesign Wizard shiny app.
#
# This shiny app displays several tabs, each of which is a separate shiny module (http://shiny.rstudio.com/articles/modules.html)
# implemented in the respective "tab_ ... .R" file. Hence this file only implements the "skeleton" of the application.
#
# Clara Bicalho <clara.bicalho@wzb.eu>
# Markus Konrad <markus.konrad@wzb.eu>
# Sisi Huang <sisi.huang@wzb.eu>
#
# Dec. 2018
#
library(DesignLibrary)
library(shiny)
library(shinymaterial)
library(shinyalert)
library(shinyBS)
library(shinyjs)
library(stringr)
library(stringi)
library(dplyr)
library(MASS)
source('conf.R')
source('common.R')
source('uihelpers.R')
source('tab_design.R')
source('tab_inspect.R')
# -------------- Frontend: User interface definition --------------
piwik_code_file <- 'www/piwik.txt'
if (file.exists(piwik_code_file)) {
piwik_code <- readChar(piwik_code_file, file.info(piwik_code_file)$size)
print('using PiWik code')
} else {
piwik_code <- ''
}
ui <- function(request) {
material_page(
# title
nav_bar_fixed = TRUE,
nav_bar_color = "white",
title = tags$img(src="brand.png", height = 52.5, width = 300),
# additional JS / CSS libraries
bootstrapLib(),
withMathJax(),
tags$head(
tags$link(rel = "stylesheet", type = "text/css", href = "custom.css"),
HTML(piwik_code),
tags$title(app_title)
),
shinyjs::useShinyjs(),
shinyjs::extendShinyjs(script = 'www/custom.js', functions = c("registerBookmarkModalClickHandler",
"unregisterBookmarkModalClickHandler")),
div(
HTML(sprintf('<a id="feedback_form" class="btn btn-sm" title="Feedback form" target="_blank" href="%s">
Give feedback</a>', feedback_form_url)),
bookmarkButton("Share",
title = "Share the status of your design and diagnoses",
class = "btn btn-sm",
icon = NULL),
id = "top_button_row"
),
# tabs
material_tabs(
tabs = c(
"Design" = "tab_design",
"Diagnose" = "tab_inspect"
),
color = "blue"
),
# "Design" tab
useShinyalert(),
designTabUI('tab_design'),
# "Inspect" tab
inspectTabUI('tab_inspect'),
#Footer
tags$footer(
actionLink("show_help_text", "Help"),
span(' | '),
actionLink('show_legal_notice', 'Legal notice'),
span(' | '),
actionLink('show_data_protection_policy', 'Data protection policy'),
align = "center", style = "
bottom:0;
width:100%;
color: black;
padding: 10px;
background-color: #F5F5F5;
z-index: 1000;"
)
)
}
# -------------- Backend: Input handling and output generation on server --------------
server <- function(input, output, session) {
insp_changed_args <- character()
design_tab_proxy <- callModule(designTab, 'tab_design')
inspect_tab_proxy <- callModule(inspectTab, 'tab_inspect', design_tab_proxy)
# -------------- observers for global events --------------
# get started button clicked
observeEvent(input$show_help_text,{
alert_with_content_from_html_file('Welcome to DDWizard', 'www/get_started.html', 'Get started')
})
# legal notice button clicked
observeEvent(input$show_legal_notice, {
alert_with_content_from_html_file('Legal notice', 'www/legal_notice.html', className = 'wide')
})
# data protection button clicked
observeEvent(input$show_data_protection_policy, {
alert_with_content_from_html_file('Data protection policy', 'www/data_protection_policy.html', className = 'wide')
})
# observe changes between tabs
observe({
if (!is.null(input$current_tab)) { # initially, currentTab is NULL; it only gets a value after the first tab change
if (input$current_tab == 'tab_inspect') { # check if change to "inspect" tab occurred
# when switching from design to inspect tab, pass the recorded arguments from below (i.e. those that the
# user changed previously in the inspect tab). we will not override these arguments' values with values
# from the design tab.
inspect_tab_proxy$set_changed_args(insp_changed_args)
} else if (input$current_tab == 'tab_design') { # check if change to "design" tab occurred
# when switching from inspect to design tab, record which arguments the user changed in the inspect tab,
# i.e. which of the values in the inspect tab differ from those in the design tab.
# when switching back to the inspect tab, we will not override these with values from the design tab.
isolate({
insp_changed_args <<- inspect_tab_proxy$get_changed_args()
})
}
}
})
# observe show intro event which is triggered on start from custom.js when no bookmark is restored
observe({
req(input$show_intro_modal)
alert_with_content_from_html_file('Welcome to DDWizard', 'www/get_started.html', 'Get started')
})
# -------------- bookmarking --------------
# handling of bookmarking via "SHARE" button on top right
onBookmark(function(state) {
print('BOOKMARKING IN APP:')
state$values$current_tab <- input$current_tab
print(state$values$current_tab)
})
onBookmarked(function(url) {
shinyalert(
title = 'Save and share',
text = sprintf('<p>You can save and share the current state of your design and diagnosis with the following link:</p>
<pre class="share-url"><code>%s</code></pre>
<div id="copy-share-url" class="btn btn-primary">Copy to clipboard</button>', url),
closeOnEsc = TRUE,
closeOnClickOutside = TRUE,
html = TRUE,
showConfirmButton = TRUE,
showCancelButton = FALSE,
confirmButtonText = "OK",
timer = 0,
imageUrl = "",
confirmButtonCol = "light-blue darken-3",
animation = TRUE,
callbackR = function(x) { shinyjs::js$unregisterBookmarkModalClickHandler(); }
)
shinyjs::js$registerBookmarkModalClickHandler();
})
onRestore(function(state) {
# open the bookmarked tab
shinymaterial::select_material_tab(session, state$values$current_tab)
})
}
# Run the application
shinyApp(ui = ui, server = server, enableBookmarking = 'server')