File: server.R

package info (click to toggle)
r-cran-shinycssloaders 1.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 560 kB
  • sloc: javascript: 75; makefile: 5
file content (53 lines) | stat: -rw-r--r-- 1,548 bytes parent folder | download
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
server <- function(input, output, session) {
  plotnum <- reactiveVal(0)

  spinner_params <- reactive({
    if (input$type == "custom") {
      params <- list(
        ui_element = plotOutput(paste0("example", plotnum())),
        image = input$image,
        image.width = if (input$image_size_default) NULL else input$image.width,
        image.height = if (input$image_size_default) NULL else input$image.height
      )
    } else {
      params <- list(
        ui_element = plotOutput(paste0("example", plotnum())),
        type = as.numeric(input$type),
        color = input$col,
        size = input$size,
        color.background = "#fafafa"
      )
    }

    if (nzchar(input$caption)) {
      params$caption <- input$caption
    }

    params
  })

  output$show_example <- renderUI({
    suppressWarnings(do.call(shinycssloaders::withSpinner, spinner_params()))
  })

  observeEvent(input$update, ignoreNULL = FALSE, {
    plotnum(plotnum() + 1)
    output[[paste0("example", plotnum())]] <- renderPlot({
      bg <- par(bg = "#fafafa")
      shinyjs::disable("params")
      on.exit(shinyjs::enable("params"))
      Sys.sleep(isolate(input$time))
      plot(runif(10), main = "Random Plot")
      bg <- par(bg = bg)
    })
  })

  observeEvent(input$show, {
    params <- spinner_params()
    params$ui_element <- NULL
    params$background <- input$bg
    suppressWarnings(do.call(shinycssloaders::showPageSpinner, params))
    Sys.sleep(input$time)
    hidePageSpinner()
  })
}