Within an observer button event, I’m trying to render an update message to the Shiny UI upon completion. In the code below, each task is timestamped correctly however they aren’t rendered to the UI until the end.
library(shiny)
ui <- fluidPage(
titlePanel("Task Renderer"),
mainPanel(
h3("Completed Tasks:"),
verbatimTextOutput("task_output"),
actionButton("process_button", "Process")
)
)
server <- function(input, output, session) {
milestones <- c("Task 1: Data collection",
"Task 2: Data cleaning",
"Task 3: Data analysis",
"Task 4: Model building",
"Task 5: Model evaluation",
"Task 6: Report generation")
current_task <- reactiveVal(0)
completed_tasks <- reactiveVal(character())
nextTask <- function() {
task_index <- current_task() + 1
if (task_index <= length(milestones)) {
current_task(task_index)
current_time <- format(Sys.time(), "%Y-%m-%d %H:%M:%S")
completed_tasks(c(completed_tasks(), paste(milestones[task_index], "-", current_time)))
invalidateLater(1000) # Trigger UI update after 1 second
}
}
output$task_output <- renderText({
task_index <- current_task()
if (task_index > 0) {
paste(completed_tasks(), collapse = "n")
} else {
"No tasks completed yet."
}
})
observeEvent(input$process_button, {
# Reset current task and completed tasks
current_task(0)
completed_tasks(character())
#simulate next bit of code
Sys.sleep(5)
nextTask() #render task complete message as each is finished
#simulate next bit of code
Sys.sleep(5)
nextTask() #render task complete message as each is finished
#simulate next bit of code
Sys.sleep(5)
nextTask() #render task complete message as each is finished
})
}
shinyApp(ui = ui, server = server)
I’ve tried moving the function in and out of the button event code block and several tries with different reactive configurations
New contributor
Summitbri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3