(Reproducible code follows). I have some processes that take a while and I want my Shiny app to give some status updates. Here's what I intended for my code to do. After the click of the go button, a file is read and each record of a table is processed (it takes a while for each record to be processed). The actual processing is replaced by a 2 second wait in the code. Before and after each record is processed the table changes and it should be refreshed. Sadly the table doesn't appear until the end of all processing.
Here are some comments / questions about my code:
I make the statusTable a reactive value. The intention is that when this changes, it will trigger an update to the table output.
I use the observe function thinking that if either of the reactive values (input$go or statusTable) change, the code will be executed.
library(DT)
library(dplyr)
library(shiny)
if (interactive()) {
ui <- fluidPage(
actionButton(inputId = "go", label = "Go"),
hr(),
renderPrint("Output below"),
DT::dataTableOutput("statusTable")
)
server <- function(input, output, session){
rv <- reactiveValues(statusTable = tibble(Name = character(0), Status = character(0), Result = character(0))) # make this a reactive value.
output$statusTable <- DT::renderDataTable(rv$statusTable)
# observeEvent(statusTable(), {
# output$statusTable <- DT::renderDataTable(rv$statusTable)
# })
observeEvent(input$go, {
# in the real world, data would be read from a file. processing would
rawData <- tibble(Name = 1:3, y = 4:6, z = 7:9)
# initial appearance of the statusTable
rv$statusTable <- tibble(Name = rawData$Name, Status = rep("Queued", nrow(rawData)), Result = rep("na", nrow(rawData)))
for (i in 1:nrow(rawData)) {
temp <- rv$statusTable
temp[i, "Status"] <- "Running"
rv$statusTable <- temp
Sys.sleep(2)
temp <- rv$statusTable
temp[i, "Status"] <- "Done"
temp[i, "Result"] <- as.character(i)
rv$statusTable <- temp
}
})
}
shinyApp(ui, server)
}