Skip to content

Instantly share code, notes, and snippets.

@denisabd
Last active October 22, 2023 10:52
Show Gist options
  • Save denisabd/4fca6e0961b48b7e8aeaaac5187a9e4d to your computer and use it in GitHub Desktop.
Save denisabd/4fca6e0961b48b7e8aeaaac5187a9e4d to your computer and use it in GitHub Desktop.
Async Progress Bar in R Shiny
library(shiny)
library(future)
library(promises)
plan(multisession, workers = 4)
ui <- fluidPage(
tags$head(
tags$style(
HTML("
.progress-bar{
background-color: #753bbd;
}
.progress-message {
padding: 0px 3px;
margin: 75px;
font-weight: bold;
font-size: 90%;
}
.shiny-notification {
position:relative;
top: -60vh;
left: -70vw;
width: 350px;
font-size: 16px;
background-color: #d1e4ff;
color: #333;
border: 1.5px solid #753bbd;
border-radius: 3px;
opacity: 1;
padding: 10px 8px 10px 10px;
margin: 2px;
}")
)
),
actionButton("show", "Show"),
actionButton("show2", "Show2")
)
server <- function(input, output, session) {
onStop(function() cat("Session stopped\n"))
observeEvent(input$show, {
detail_text <- sample(
c(
"Use frequency keywords 'monthly', 'weekly' to get time series data",
"More information about the platform use can be found on Info tab or website",
"Platform is a real-time analytics platform built by the Data team",
"You can upload your own data in csv or xlsx (first tab)",
"Save output by downloading a csv file or interactive HTML report",
"Use 'average' keyword when working with measures like 'Cycle Time'"
),
size = 1)
progress <- ipc::AsyncProgress$new(session,
message = "10% Starting Engine",
min = 0,
max = 100,
value = 10,
detail = detail_text
)
Sys.sleep(0.5)
promises::future_promise({
for (i in 10:99) {
progress$set(value = i,
message = dplyr::case_when(
i < 40 ~ paste0(i, "% Retrieving Data"),
i < 70 ~ paste0(i, "% Rendering Output"),
i < 90 ~ paste0(i, "% Writing Narratives"),
TRUE ~ paste0(i, "% Finalizing Search")
)
)
Sys.sleep(5/90)
}
#progress$close()
})
cli::cat_rule("Progress Bar 1")
})
observeEvent(input$show2,{
progress2 <- ipc::AsyncProgress$new(session,
message = "Progress Bar 2",
min = 0,
max = 100,
value = 10
)
future_promise({
Sys.sleep(3)
#progress2$close()
})
cli::cat_rule("Progress Bar 2")
})
}
shinyApp(ui, server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment