下面的应用程序包含一个actionButton
、一个shinyWidgets::progressBar
和一个selectInput
:
Start
单击按钮时,observeEvent
会触发 an ,其中我循环遍历数字 1-10 并在每次迭代时递增进度条。我还想selectInput
在每次迭代时更新值,但 updateSelectInput 不能按预期工作。selectInput
该值仅在循环终止后才更新,而不是与进度条同步更新。我不明白为什么updateProgressBar
在这里工作但updateSelectInput
没有?
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
actionButton(inputId = "go", label = "Start"), #, onclick = "$('#my-modal').modal().focus();"
shinyWidgets::progressBar(id = "pb", value = 0, display_pct = TRUE),
selectInput('letters', 'choose', letters)
)
server <- function(input, output, session) {
observeEvent(input$go, {
shinyWidgets::updateProgressBar(session = session, id = "pb", value = 0) # reinitialize to 0 if you run the calculation several times
for (i in 1:10) {
updateProgressBar(session = session, id = "pb", value = 100/10*i)
updateSelectInput(session, 'letters', selected = letters[i])
Sys.sleep(.5)
}
})
}
shinyApp(ui = ui, server = server)