Debian Codegraph @

inst/shiny/along/app.R

raw
1 # modified from https://shiny.rstudio.com/articles/progress.html
2
3 # This example uses `cli_progress_along()`.
4
5 library(cli)
6
7 # !!! You don't need these in real code.
8 # cli.progress_show_after makes sure that we see the progress bar from
9 # the beginning, not only after a delay.
10 options(cli.progress_show_after = 0)
11
12 # !!! You don't need these in real code.
13 # This also requests logging the progress bar to the standard output,
14 # in the console.
15 options(cli.progress_handlers_only = c("shiny", "logger"))
16
17 server <- function(input, output) {
18 output$plot <- renderPlot({
19 input$goPlot # Re-run when button is clicked
20
21 # Create 0-row data frame which will be used to store data
22 dat <- data.frame(x = numeric(0), y = numeric(0))
23
24 # Number of times we'll go through the loop
25 n <- 10
26
27 for (i in cli_progress_along(1:n, "Rendering", format = "At {i}")) {
28 # Each time through the loop, add another row of data. This is
29 # a stand-in for a long-running computation.
30 dat <- rbind(dat, data.frame(x = rnorm(1), y = rnorm(1)))
31
32 # Pause for 0.1 seconds to simulate a long computation.
33 Sys.sleep(0.5)
34 }
35
36 plot(dat$x, dat$y)
37 })
38 }
39
40 ui <- shinyUI(basicPage(
41 plotOutput('plot', width = "300px", height = "300px"),
42 actionButton('goPlot', 'Go plot')
43 ))
44
45 shinyApp(ui = ui, server = server)