Open mnist91 opened 3 years ago
Hi @mnist91, I did some digging and I believe this is caused by an unnecessary reset deep within Shiny's javascript code where selectizejs is called. A direct fix would be a lot of work, but you can work around the symptoms by manually "debouncing" the selectizeInput
's return value whenever you are using the update function:
library(shiny)
ui <- fluidPage(
mainPanel(
checkboxInput("use_server", "use server-side selectizeInput()", TRUE),
checkboxInput("use_debounce", "debounce server-side selectizeInput()", FALSE),
selectizeInput("inp", "Input", choices = letters, selected = 1),
verbatimTextOutput ("log"),
actionButton("btn", "update selectize"),
)
)
server <- function(input, output, session) {
ignore_values <- reactiveVal(FALSE)
# trigger selectize update on button click
observeEvent(input$btn, {
if (input$use_server && input$use_debounce) ignore_values(TRUE)
updateSelectizeInput(inputId = "inp",
choices = letters,
selected = sample(letters, 1),
server = input$use_server)
})
# print previous values to UI
previous_values <- reactiveVal(c())
observeEvent(input$inp, {
if (ignore_values()) {
ignore_values(FALSE) # ignore update only once
} else {
previous_values <- previous_values()
len <- min(15, length(previous_values))
previous_values( c(input$inp, previous_values[0:len]) )
}
})
output$log <- renderPrint({ previous_values() })
}
shinyApp(ui = ui, server = server)
Unfortunately, this will still cause the Input Element to "flicker" when using the update function.
I hope this gets fixed on Shiny's end eventually. There seem to be quite a few issues with selectizeInput()
.
Bug Description
updateSelectizeInput()
does change its updated value twice ifserver = TRUE
. First, it sets the selected value to""
and afterwards updates the value. This causes the reactive chain to trigger twice.You can see this behavior by changing "Input 1" and checking the difference in the print-result whether the checkbox is ticked or not. There should not be an empty Input 2 in my opinion.
Example application
Logs
System details
Output of
sessionInfo()
: