-
Notifications
You must be signed in to change notification settings - Fork 0
/
golem_utils_ui.R
466 lines (442 loc) · 8.42 KB
/
golem_utils_ui.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#' Turn an R list into an HTML list
#'
#' @param list An R list
#' @param class a class for the list
#'
#' @return an HTML list
#'
#' @export list_to_li
#'
#' @examples
#' list_to_li(c("a", "b"))
#' @importFrom shiny tags tagAppendAttributes tagList
list_to_li <- function(list, class = NULL) {
if (is.null(class)) {
tagList(
lapply(
list,
tags$li
)
)
} else {
res <- lapply(
list,
tags$li
)
res <- lapply(
res,
function(x) {
tagAppendAttributes(
x,
class = class
)
}
)
tagList(res)
}
}
#' Turn an R list into corresponding HTML paragraph tags
#'
#' @param list an R list
#' @param class a class for the paragraph tags
#'
#' @return An HTML tag
#'
#' @export list_to_p
#'
#' @examples
#' list_to_p(c("This is the first paragraph", "this is the second paragraph"))
#' @importFrom shiny tags tagAppendAttributes tagList
#'
list_to_p <- function(list, class = NULL) {
if (is.null(class)) {
tagList(
lapply(
list,
tags$p
)
)
} else {
res <- lapply(
list,
tags$p
)
res <- lapply(
res,
function(x) {
tagAppendAttributes(
x,
class = class
)
}
)
tagList(res)
}
}
#' Names list
#'
#' @export named_to_li
#'
#' @param list a list
#' @param class class of object
#'
#' @importFrom shiny tags tagAppendAttributes tagList
named_to_li <- function(list, class = NULL) {
if (is.null(class)) {
res <- mapply(
function(x, y) {
tags$li(
HTML(
sprintf("<b>%s:</b> %s", y, x)
)
)
},
list,
names(list),
SIMPLIFY = FALSE
)
tagList(res)
} else {
res <- mapply(
function(x, y) {
tags$li(
HTML(
sprintf("<b>%s:</b> %s", y, x)
)
)
},
list,
names(list),
SIMPLIFY = FALSE
)
res <- lapply(
res,
function(x) {
tagAppendAttributes(
x,
class = class
)
}
)
tagList(res)
}
}
#' Remove a tag attribute
#'
#' @param tag the tag
#' @param ... the attributes to remove
#'
#' @return a new tag
#'
#' @export tagRemoveAttributes
#'
#' @examples
#' a <- shiny::tags$p(src = "plop", "pouet")
#' tagRemoveAttributes(a, "src")
tagRemoveAttributes <- function(tag, ...) {
attrs <- as.character(list(...))
for (i in seq_along(attrs)) {
tag$attribs[[attrs[i]]] <- NULL
}
tag
}
#' Hide or display a tag
#'
#' @param tag the tag
#'
#' @return a tag
#'
#' @export undisplay
#'
#' @examples
#' ## Hide
#' a <- shiny::tags$p(src = "plop", "pouet")
#' undisplay(a)
#' b <- shiny::actionButton("go_filter", "go")
#' undisplay(b)
#' @importFrom shiny tagList
undisplay <- function(tag) {
# if not already hidden
if (
!is.null(tag$attribs$style) &&
!grepl("display:\\s+none", tag$attribs$style)
) {
tag$attribs$style <- paste(
"display: none;",
tag$attribs$style
)
} else {
tag$attribs$style <- "display: none;"
}
tag
}
#' Display
#'
#' @export display
#'
#' @param tag html tag
#'
#' @importFrom shiny tagList
display <- function(tag) {
if (
!is.null(tag$attribs$style) &&
grepl("display:\\s+none", tag$attribs$style)
) {
tag$attribs$style <- gsub(
"(\\s)*display:(\\s)*none(\\s)*(;)*(\\s)*",
"",
tag$attribs$style
)
}
tag
}
#' Hide an elements by calling jquery hide on it
#'
#' @param id the id of the element to hide
#'
#' @export jq_hide
#'
#' @importFrom shiny tags
jq_hide <- function(id) {
tags$script(sprintf("$('#%s').hide()", id))
}
#' Add a red star at the end of the text
#'
#' Adds a red star at the end of the text
#' (for example for indicating mandatory fields).
#'
#' @param text the HTLM text to put before the red star
#'
#' @return an html element
#'
#' @export with_red_star
#'
#' @examples
#' with_red_star("Enter your name here")
#' @importFrom shiny tags HTML
with_red_star <- function(text) {
shiny::tags$span(
HTML(
paste0(
text,
shiny::tags$span(
style = "color:red",
"*"
)
)
)
)
}
#' Repeat tags$br
#'
#' @param times the number of br to return
#'
#' @return the number of br specified in times
#'
#' @export
#'
#' @examples
#' rep_br(5)
#' @importFrom shiny HTML
rep_br <- function(times = 1) {
HTML(rep("<br/>", times = times))
}
#' Create an url
#'
#' @param url the URL
#' @param text the text to display
#'
#' @return an a tag
#'
#' @export
#'
#' @examples
#' enurl("https://www.thinkr.fr", "ThinkR")
#' @importFrom shiny tags
enurl <- function(url, text) {
tags$a(href = url, text)
}
#' Column wrapper
#'
#' These are convenient wrappers around
#' `column(12, ...)`
#'
#' @param ... arguments from `shiny::column()`
#'
#' @export
#'
#' @importFrom shiny column
col_12 <- function(...) {
column(12, ...)
}
#' Column wrapper (10)
#'
#' @param ... arguments from `shiny::column()`
#'
#' @export
#'
#' @importFrom shiny column
col_10 <- function(...) {
column(10, ...)
}
#' Column wrapper (8)
#'
#' @param ... arguments from `shiny::column()`
#'
#' @export
#'
#' @importFrom shiny column
col_8 <- function(...) {
column(8, ...)
}
#' Column wrapper (6)
#'
#' @param ... arguments from `shiny::column()`
#'
#' @export
#'
#' @importFrom shiny column
col_6 <- function(...) {
column(6, ...)
}
#' Column wrapper (4)
#'
#' @param ... arguments from `shiny::column()`
#'
#' @export
#'
#' @importFrom shiny column
col_4 <- function(...) {
column(4, ...)
}
#' Column wrapper (3)
#'
#' @param ... arguments from `shiny::column()`
#'
#' @export
#'
#' @importFrom shiny column
col_3 <- function(...) {
column(3, ...)
}
#' Column wrapper (2)
#'
#' @param ... arguments from `shiny::column()`
#'
#' @export
#'
#' @importFrom shiny column
col_2 <- function(...) {
column(2, ...)
}
#' Column wrapper (1)
#'
#' @param ... arguments from `shiny::column()`
#'
#' @export
#'
#' @importFrom shiny column
col_1 <- function(...) {
column(1, ...)
}
#' Make the current tag behave like an action button
#'
#' Only works with compatible tags like button or links
#'
#' @param tag Any compatible tag.
#' @param inputId Unique id. This will host the input value to be used
#' on the server side.
#'
#' @return The modified tag with an extra id and the action button class.
#'
#' @export
#'
#' @examples
#' if (interactive()) {
#' library(shiny)
#'
#' link <- a(href = "#", "My super link", style = "color: lightblue;")
#'
#' ui <- fluidPage(
#' make_action_button(link, inputId = "mylink")
#' )
#'
#' server <- function(input, output, session) {
#' observeEvent(input$mylink, {
#' showNotification("Pouic!")
#' })
#' }
#'
#' shinyApp(ui, server)
#' }
make_action_button <- function(tag, inputId = NULL) {
# some obvious checks
if (!inherits(tag, "shiny.tag")) stop("Must provide a shiny tag.")
if (!is.null(tag$attribs$class)) {
if (grep("action-button", tag$attribs$class)) {
stop("tag is already an action button")
}
}
if (is.null(inputId) && is.null(tag$attribs$id)) {
stop("tag does not have any id. Please use inputId to be able to
access it on the server side.")
}
# handle id
if (!is.null(inputId)) {
if (!is.null(tag$attribs$id)) {
warning(
paste(
"tag already has an id. Please use input$",
tag$attribs$id,
"to access it from the server side. inputId will be ignored."
)
)
} else {
tag$attribs$id <- inputId
}
}
# handle class
if (is.null(tag$attribs$class)) {
tag$attribs$class <- "action-button"
} else {
tag$attribs$class <- paste(tag$attribs$class, "action-button")
}
# return tag
tag
}
# UNCOMMENT AND USE
#
# attachment::att_amend_desc()
#
# To use this part of the UI
#
#' #' Include Content From a File
#' #'
#' #' Load rendered RMarkdown from a file and turn into HTML.
#' #'
#' #' @rdname includeRMarkdown
#' #'
#' #' @export includeRMarkdown
#' #'
#' #' @importFrom rmarkdown render
#' #' @importFrom markdown markdownToHTML
#' #' @importFrom shiny HTML
#' includeRMarkdown <- function(path){
#'
#' md <- tempfile(fileext = '.md')
#'
#' on.exit(unlink(md),add = TRUE)
#'
#' rmarkdown::render(
#' path,
#' output_format = 'md_document',
#' output_dir = tempdir(),
#' output_file = md,quiet = TRUE
#' )
#'
#' html <- markdown::markdownToHTML(md, fragment.only = TRUE)
#'
#' Encoding(html) <- "UTF-8"
#'
#' return(HTML(html))
#' }