/
busy-modal.R
275 lines (249 loc) · 7.57 KB
/
busy-modal.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
#' @title Show a modal with a spinner
#'
#' @description Make a pop-up window appear from the server
#' with a spinner during long computation, remove it when finished.
#'
#' @param spin Style of the spinner, see [spin_epic()] or [spin_kit()] for possible choices.
#' @param color Color for the spinner, in a valid CSS format.
#' @param text Additional text to appear under the spinner.
#' @param session The `session` object passed to function given to `shinyServer`.
#'
#' @export
#'
#' @importFrom shiny showModal modalDialog getDefaultReactiveDomain
#'
#' @name modal-spinner
#'
#' @example examples/modal_spinner.R
show_modal_spinner <- function(spin = "double-bounce",
color = "#112446",
text = NULL,
session = shiny::getDefaultReactiveDomain()) {
spin <- match.arg(arg = spin, choices = c(spinkit_spinners(), epic_spinners()))
if (spin %in% spinkit_spinners()) {
tag_spin <- spin_kit(spin = spin, color = color)
} else {
tag_spin <- spin_epic(spin = spin, color = color)
}
showModal(modalDialog(
class = "shinybusy-modal",
js_center_modal(),
html_dependency_spinkit(),
html_dependency_epic(),
tags$div(
class = "shinybusy-modal-spinner",
style = "width: 60px; height: 60px; position: relative; margin: auto;",
tag_spin
),
tags$div(
class = "shinybusy-modal-text",
style = "text-align: center;",
text
),
footer = NULL,
easyClose = FALSE,
fade = FALSE,
size = "s"
), session = session)
}
#' @export
#' @importFrom shiny removeModal
#' @rdname modal-spinner
remove_modal_spinner <- shiny::removeModal
#' @export
#' @importFrom shiny getDefaultReactiveDomain removeUI insertUI
#' @rdname modal-spinner
update_modal_spinner <- function(text, session = shiny::getDefaultReactiveDomain()) {
removeUI(selector = ".shinybusy-modal-text", immediate = TRUE, session = session)
insertUI(
selector = ".shinybusy-modal",
where = "beforeEnd",
immediate = TRUE,
session = session,
ui = tags$div(
class = "shinybusy-modal-text",
style = "text-align: center;",
text
)
)
}
#' @title Show a modal with a progress bar
#'
#' @description Make a pop-up window appear from the server
#' with a spinner during long computation, remove it when finished.
#'
#' @inheritParams progress
#' @param session The `session` object passed to function given to `shinyServer`.
#'
#' @export
#'
#' @importFrom shiny showModal modalDialog getDefaultReactiveDomain
#'
#' @name modal-progress
#'
#' @example examples/modal_progress.R
show_modal_progress_line <- function(value = 0, text = "auto",
color = "#112446",
stroke_width = 4,
easing = "linear",
duration = 1000,
trail_color = "#eee",
trail_width = 1,
height = "15px",
session = shiny::getDefaultReactiveDomain()) {
showModal(modalDialog(
class = "shinybusy-modal",
js_center_modal(),
progress_line(
value = value,
text = text,
color = color,
stroke_width = stroke_width,
easing = easing,
duration = duration,
trail_color = trail_color,
trail_width = trail_width,
height = height,
shiny_id = session$ns("shinybusy-modal-progress")
),
footer = NULL,
easyClose = FALSE,
fade = FALSE,
size = "m"
), session = session)
}
#' @export
#' @rdname modal-progress
show_modal_progress_circle <- function(value = 0, text = "auto",
color = "#112446",
stroke_width = 4,
easing = "linear",
duration = 1000,
trail_color = "#eee",
trail_width = 1,
height = "200px",
session = shiny::getDefaultReactiveDomain()) {
showModal(modalDialog(
class = "shinybusy-modal",
js_center_modal(translateY = "-60%"),
progress_circle(
value = value,
text = text,
color = color,
stroke_width = stroke_width,
easing = easing,
duration = duration,
trail_color = trail_color,
trail_width = trail_width,
height = height, width = height,
shiny_id = session$ns("shinybusy-modal-progress")
),
footer = NULL,
easyClose = FALSE,
fade = FALSE,
size = "m"
), session = session)
}
#' @export
#' @importFrom shiny removeModal
#' @rdname modal-progress
remove_modal_progress <- shiny::removeModal
#' @export
#' @rdname modal-progress
update_modal_progress <- function(value,
text = NULL,
session = shiny::getDefaultReactiveDomain()) {
update_progress(
shiny_id = "shinybusy-modal-progress",
value = value,
text = text,
session = session
)
}
#' @title Show a modal with a GIF
#'
#' @description Make a pop-up window appear from the server
#' with a GIF during long computation, remove it when finished.
#'
#' @param src Path to the GIF, an URL or a file in www/ folder.
#' @param text Additional text to appear under the spinner.
#' @param height,width Height and width of the spinner, default to `'50px'` for both, must be specified.
#' @param modal_size One of `"s"` for small (the default), `"m"` for medium, or `"l"` for large.
#' @param session The `session` object passed to function given to `shinyServer`.
#'
#' @export
#'
#' @importFrom shiny showModal modalDialog getDefaultReactiveDomain
#' @importFrom htmltools validateCssUnit
#'
#' @name modal-gif
#'
#' @examples
#' if (interactive()) {
#'
#' library(shiny)
#' library(shinybusy)
#'
#' ui <- fluidPage(
#'
#' tags$h1("Modal with spinner"),
#' actionButton("sleep1", "Launch a long calculation"),
#' actionButton("sleep2", "And another one")
#' )
#'
#' server <- function(input, output, session) {
#'
#' observeEvent(input$sleep1, {
#' show_modal_gif(
#' src = "https://jeroen.github.io/images/banana.gif"
#' )
#' Sys.sleep(5)
#' remove_modal_gif()
#' })
#'
#' observeEvent(input$sleep2, {
#' show_modal_gif(
#' src = "https://jeroen.github.io/images/banana.gif",
#' width = "300px", height = "300px",
#' modal_size = "m",
#' text = "Please wait..."
#' )
#' Sys.sleep(5)
#' remove_modal_gif()
#' })
#'
#' }
#'
#' shinyApp(ui, server)
#'
#' }
show_modal_gif <- function(src,
text = NULL,
height = "100px", width = "100px",
modal_size = "s",
session = shiny::getDefaultReactiveDomain()) {
showModal(modalDialog(
class = "shinybusy-modal",
style = "text-align: center;",
js_center_modal(),
tags$img(
class = "shinybusy-modal-gif",
style = paste0("height:", validateCssUnit(height), ";"),
style = paste0("width:", validateCssUnit(width), ";"),
src = src
),
tags$div(
class = "shinybusy-modal-text",
style = "text-align: center;",
text
),
footer = NULL,
easyClose = FALSE,
fade = FALSE,
size = modal_size
), session = session)
}
#' @export
#' @importFrom shiny removeModal
#' @rdname modal-gif
remove_modal_gif <- shiny::removeModal