Debian Codegraph @

inst/themer-demo/app.R

raw
1 library(shiny)
2 library(ggplot2)
3 library(bslib)
4 library(rlang)
5 library(curl)
6
7 # enlarged auto fonts
8 if (is_installed("thematic")) {
9 thematic::thematic_shiny(
10 font = thematic::font_spec("auto", scale = 2, update = TRUE)
11 )
12 }
13
14 # Source in ggplot2 examples
15 source("global.R")
16
17 theme <- bs_global_get() %||% bs_theme()
18 if ("3" %in% theme_version(theme)) {
19 warning("This example app requires Bootstrap 4 or higher", call. = FALSE)
20 }
21
22 rounded <- isTRUE(
23 as.logical(bs_get_variables(theme %||% bslib::bs_theme(), "enable-rounded"))
24 )
25 pill <- function(...) {
26 shiny::tabPanel(..., class = "p-3 border", class = if (rounded) "rounded")
27 }
28 tab <- function(...) {
29 shiny::tabPanel(
30 ...,
31 class = "p-3 border border-top-0",
32 class = if (rounded) "rounded-bottom"
33 )
34 }
35 gradient <- function(theme_color = "primary") {
36 bg_color <- paste0("bg-", theme_color)
37 bgg_color <- if ("4" %in% theme_version(theme)) {
38 paste0("bg-gradient-", theme_color)
39 } else {
40 paste(bg_color, "bg-gradient")
41 }
42 bg_div <- function(color_class, ...) {
43 display_classes <- paste(
44 paste0(".", strsplit(color_class, "\\s+")[[1]]),
45 collapse = " "
46 )
47 div(
48 class = "p-3",
49 class = color_class,
50 display_classes,
51 ...
52 )
53 }
54 fluidRow(
55 column(6, bg_div(bg_color)),
56 column(6, bg_div(bgg_color))
57 )
58 }
59
60 theme_colors <- c(
61 "primary",
62 "secondary",
63 "default",
64 "success",
65 "info",
66 "warning",
67 "danger",
68 "dark"
69 )
70 gradients <- lapply(theme_colors, gradient)
71
72 progressBar <- div(
73 class = "progress",
74 div(
75 class = "progress-bar w-25",
76 role = "progressbar",
77 "aria-valuenow" = "25",
78 "aria-valuemin" = "0",
79 "aria-valuemax" = "100"
80 )
81 )
82
83 # This is here for shinycoreci to take advantage of (so we don't need to update a bunch of screenshots)
84 IS_LEGACY <- as.logical(Sys.getenv("BSLIB_LEGACY_THEMER_APP", FALSE))
85 is_legacy_version <- theme_version(theme) %in% c("3", "4")
86 if (isTRUE(IS_LEGACY || is_legacy_version)) {
87 dashboardTab <- NULL
88 } else {
89 dashboardTab <- nav_panel("Dashboard", tipsUI("tips"))
90 theme_set(theme_minimal())
91 }
92
93 layout_buttons <- if (is_legacy_version) tags$div else layout_columns
94
95 bs_table <- function(x, class = NULL, ...) {
96 class <- paste(c("table", class), collapse = " ")
97 class <- sprintf('class="%s"', class)
98 HTML(knitr::kable(x, format = "html", table.attr = class))
99 }
100
101 shinyApp(
102 page_navbar(
103 theme = theme,
104 title = "Theme demo",
105 collapsible = TRUE,
106 inverse = !IS_LEGACY,
107 id = "navbar",
108 fillable = "Dashboard",
109 dashboardTab,
110 nav_panel(
111 "Inputs",
112 tabsetPanel(
113 type = "pills",
114 id = "inputs",
115 pill(
116 "inputPanel()",
117 inputPanel(
118 sliderInput(
119 "slider",
120 "sliderInput()",
121 min = 0,
122 max = 100,
123 value = c(30, 70),
124 step = 20
125 ),
126 selectInput("selectize", "selectizeInput()", choices = state.abb),
127 selectInput(
128 "selectizeMulti",
129 "selectizeInput(multiple=T)",
130 choices = state.abb,
131 multiple = TRUE
132 ),
133 dateInput("date", "dateInput()", value = "2020-12-24"),
134 dateRangeInput(
135 "dateRange",
136 "dateRangeInput()",
137 start = "2020-12-24",
138 end = "2020-12-31"
139 )
140 ),
141 br(),
142 textOutput("inputPanelOutputHeader"),
143 verbatimTextOutput("inputPanelOutput"),
144 br(),
145 tags$p(
146 "Here are some",
147 tags$code("actionButton()"),
148 "s demonstrating different theme (i.e., accent) colors"
149 ),
150 layout_buttons(
151 # Regular <div> if IS_LEGACY, otherwise layout_columns
152 actionButton(
153 "primary",
154 "Primary",
155 icon("product-hunt"),
156 class = "btn-primary m-2"
157 ),
158 actionButton("secondary", "Secondary (default)", class = "m-2"),
159 actionButton(
160 "success",
161 "Success",
162 icon("check"),
163 class = "btn-success m-2"
164 ),
165 actionButton("info", "Info", icon("info"), class = "btn-info m-2"),
166 actionButton(
167 "warning",
168 "warning",
169 icon("exclamation"),
170 class = "btn-warning m-2"
171 ),
172 actionButton(
173 "danger",
174 "Danger",
175 icon(
176 if (packageVersion("fontawesome") >= "0.3") {
177 "triangle-exclamation"
178 } else {
179 "exclamation-triangle"
180 }
181 ),
182 class = "btn-danger m-2"
183 ),
184 actionButton("dark", "Dark", icon("moon"), class = "btn-dark m-2"),
185 actionButton("light", "Light", icon("sun"), class = "btn-light m-2")
186 )
187 ),
188 pill(
189 "wellPanel()",
190 wellPanel(
191 fluidRow(
192 column(
193 6,
194 selectInput(
195 "select",
196 "selectInput()",
197 choices = state.abb,
198 selectize = FALSE
199 ),
200 selectInput(
201 "selectMulti",
202 "selectInput(multiple=T)",
203 choices = state.abb,
204 multiple = TRUE,
205 selectize = FALSE
206 ),
207 textInput(
208 "text",
209 "textInput()",
210 placeholder = "Enter some text"
211 ),
212 numericInput("numeric", "numericInput()", value = 0)
213 ),
214 column(
215 6,
216 passwordInput("password", "passwordInput()", "secret"),
217 textAreaInput(
218 "textArea",
219 "textAreaInput()",
220 placeholder = "A text area"
221 ),
222 checkboxInput("check", "checkboxInput()", value = TRUE),
223 checkboxGroupInput(
224 "checkGroup",
225 "checkboxGroupInput()",
226 choices = c("A", "B")
227 ),
228 radioButtons(
229 "radioButtons",
230 "radioButtons()",
231 choices = c("A", "B")
232 )
233 )
234 )
235 ),
236 br(),
237 textOutput("wellPanelOutputHeader"),
238 br(),
239 verbatimTextOutput("wellPanelOutput")
240 )
241 )
242 ),
243 nav_panel(
244 "Plots",
245 uiOutput("thematic_needed"),
246 plotOutput("plot"),
247 selectizeInput(
248 "plot_example",
249 "Choose an example",
250 selected = "GeomSmooth",
251 choices = list(
252 ggplot2 = names(ggplot2_examples),
253 lattice = names(lattice_examples),
254 base = names(base_examples)
255 )
256 )
257 ),
258 nav_panel(
259 "Tables",
260 tabsetPanel(
261 id = "tables",
262 tab(
263 "DataTables",
264 DT::dataTableOutput("DT")
265 ),
266 tab(
267 "Bootstrap",
268 h3("Plain tables", class = "mt-2 mb-1"),
269 bs_table(mtcars[1:5, 1:5]),
270 h3("Striped tables", class = "mt-4 mb-1"),
271 bs_table(mtcars[1:5, 1:5], class = "table-striped"),
272 h3("Striped tables with hover", class = "mt-4 mb-1"),
273 bs_table(mtcars[1:5, 1:5], class = "table-striped table-hover")
274 )
275 )
276 ),
277 nav_panel(
278 "Notifications",
279 tabsetPanel(
280 id = "otherNav",
281 tab(
282 "Messages",
283 br(),
284 actionButton("showProgress", "Progress", style = "margin: 1rem"),
285 actionButton("showModal", "Modals", style = "margin: 1rem"),
286 lapply(c("default", "message", "warning", "error"), function(x) {
287 X <- tools::toTitleCase(x)
288 class <- switch(
289 x,
290 message = "btn-info",
291 warning = "btn-warning",
292 error = "btn-danger"
293 )
294 actionButton(
295 paste0("show", X),
296 paste(X, "notification"),
297 class = class,
298 style = "margin: 1rem"
299 )
300 })
301 ),
302 tab(
303 "Uploads & Downloads",
304 br(),
305 fileInput("file", "fileInput()"),
306 downloadButton(
307 "downloadButton",
308 "downloadButton()",
309 style = "margin: 1rem"
310 ),
311 downloadLink("downloadLink", "downloadLink()", style = "margin: 1rem")
312 )
313 )
314 ),
315 nav_panel(
316 "Fonts",
317 h1("Heading font:", class = "text-primary"),
318 hr(class = "bg-primary", style = "height: 5px"),
319 h1("Heading 1"),
320 h2("Heading 2"),
321 h3("Heading 3"),
322 h4("Heading 4"),
323 h5("Heading 5"),
324 h1("Base font:", class = "text-primary"),
325 hr(class = "bg-primary", style = "height: 5px"),
326 tags$p(
327 "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
328 ),
329 h1("Code font:", class = "text-primary"),
330 hr(class = "bg-primary", style = "height: 5px"),
331 tags$pre(
332 class = "shiny-text-output",
333 style = "white-space: pre-wrap",
334 "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
335 )
336 ),
337 nav_panel(
338 "Options",
339 if ("4" %in% theme_version(theme)) {
340 p(
341 "Background color gradients are disabled by default.",
342 "Enable them to see the difference here.",
343 "If enabled, gradients automatically apply buttons and progress bars, ",
344 "but you may also add to a .bg-gradient-* modified class to arbitrary elements."
345 )
346 } else {
347 markdown(
348 "`{bslib}` 'extends' Bootstrap's [`background-color` utility classes](https://getbootstrap.com/docs/5.0/utilities/background/) to also sets the foreground `color` to a sensible contrasting color (i.e., no additional `text-*` classes are needed)."
349 )
350 },
351 !!!gradients,
352 br(),
353 tags$p(
354 "With the default settings, enabling of box shadows adds a very subtle and
355 barely noticable inner box-shadow to most input widgets. The difference is
356 a little more obvious in a progress bar:"
357 ),
358 progressBar,
359 br(),
360 tags$p(
361 "Rounded corners are enabled by default and apply to numerous components (e.g., ",
362 code("tabsetPanel()"),
363 ",",
364 code("wellPanel()"),
365 ", and ",
366 code("actionButton()"),
367 "):",
368 actionButton("showProgress2", "Show progress")
369 ),
370 br(),
371 tags$p(
372 "Smooth transitions are enabled by default, but if you disable them, progress updating",
373 "in ",
374 code("fileInput()"),
375 "and ",
376 code("Progress"),
377 " will appear more staggered",
378 "(e.g., click button above)."
379 )
380 )
381 ),
382 function(input, output, session) {
383 output$DT <- DT::renderDataTable({
384 cars <- mtcars
385 for (var in c("cyl", "vs", "am", "gear", "carb")) {
386 cars[[var]] <- factor(cars[[var]])
387 }
388
389 DT::datatable(
390 cars,
391 style = "auto",
392 caption = "A generic table made with the DT package and the DataTables library.",
393 filter = "top"
394 )
395 })
396
397 output$inputPanelOutputHeader <- renderText({
398 "Below are the values bound to each input widget above"
399 })
400
401 output$inputPanelOutput <- renderPrint({
402 str(
403 list(
404 sliderInput = input$slider,
405 selectizeInput = input$selectize,
406 selectizeMultiInput = input$selectizeMulti,
407 dateInput = input$date,
408 dateRangeInput = input$dateRange
409 )
410 )
411 })
412
413 output$wellPanelOutputHeader <- renderText({
414 "Below are the values bound to each input widget above"
415 })
416
417 output$wellPanelOutput <- renderPrint({
418 str(
419 list(
420 selectInput = input$select,
421 selectMultiInput = input$selectMulti,
422 textInput = input$text,
423 numericInput = input$numeric,
424 passwordInput = input$password,
425 textAreaInput = input$textArea,
426 checkInput = input$check,
427 checkGroupInput = input$checkGroup,
428 radioButtonsInput = input$radioButtons
429 )
430 )
431 })
432
433 observeEvent(input$showModal, {
434 showModal(
435 modalDialog(
436 title = "Somewhat important message",
437 "This is a somewhat important message.",
438 easyClose = TRUE,
439 footer = modalButton("Close")
440 )
441 )
442 })
443
444 fake_progress <- function(style = "notification") {
445 withProgress(
446 message = 'Calculation in progress',
447 detail = 'This may take a while...',
448 value = 0,
449 style = style,
450 {
451 for (i in 1:15) {
452 incProgress(
453 amount = 1 / 15,
454 detail = paste("Step", i, "of", 15)
455 )
456 Sys.sleep(0.25)
457 }
458 }
459 )
460 }
461
462 observeEvent(input$showProgress, {
463 fake_progress()
464 # TODO: old progress styling could be improved
465 #fake_progress("old")
466 })
467
468 observeEvent(input$showProgress2, {
469 p <- Progress$new()
470 p$set(
471 message = 'Calculation in progress',
472 detail = 'This may take a while...',
473 value = 0.5
474 )
475 })
476
477 lapply(c("default", "message", "warning", "error"), function(x) {
478 X <- tools::toTitleCase(x)
479 observeEvent(input[[paste0("show", X)]], {
480 q <- parseQueryString(session$clientData$url_search)
481 if ("notification-duration" %in% names(q)) {
482 duration <- as.numeric(q[["notification-duration"]])
483 if (duration <= 0) duration <- NULL
484 } else {
485 duration <- 5
486 }
487 showNotification(
488 paste(X, "notification styling"),
489 type = x,
490 duration = duration
491 )
492 })
493 })
494
495 output$plot <- renderPlot({
496 ggplot2_examples[[input$plot_example]] %||%
497 eval(lattice_examples[[input$plot_example]]) %||%
498 eval(base_examples[[input$plot_example]])
499 })
500
501 output$thematic_needed <- renderUI({
502 if (bslib:::is_installed("thematic")) {
503 return(NULL)
504 }
505
506 htmltools::HTML(
507 "<span class=\"bg-warning\">&nbsp;!! Install the <a href='https://rstudio.github.io/thematic/'><code>thematic</code></a> package to enable auto-theming of static R plots !!&nbsp;</span>"
508 )
509 })
510
511 if (!IS_LEGACY) tipsServer("tips")
512 }
513 )