Here is a simple reproducible example:
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
pickerInput("test",choices=list("A"=c(1,2,3,4,5),"B"=c(6,7,8,9,10)),multiple=TRUE),
textOutput("testOutput")
)
server <- function(input, output) {
output$testOutput <- renderText({paste(input$test)})
}
shinyApp(ui = ui, server = server)
What I'd like is to click A and have the pickerInput automatically select 1,2,3,4 and 5. Or if we click B, it automatically selects 6,7,8,9, and 10.
Desired output after clicking "A":
Any help is appreciated, thanks.
You can use some
JS
to get the result:Explanation
Idea is that you set an
onClick
event for the header line, where you set aninput
variable, upon which you can react inShiny
.The whole
MutationObserver
construct is a crude workaround, because I could not get a (delegated) event listener working.What I observed is that (not bring an
JavaScript
specialist):$('.dropdown-header').on()
woudl not work, because the element is not yet existing.$(document).on('click', '.dropdown-header', ...)
did not work either. I assume that somewhere there is astopPropagation
preventing that the event is bubbling up.Thus, I used the
MutationObserver
to add the('.drodown-header')
listener the moment it is created. Not the most beautiful nor a resource preserving solution, but at least a working one. Maybe, you can find out how to properly set the event listener w/o theMutationObsever
.Update
If you want to keep all existing selections, you would change the
observeEvent
as follows:More Background 2022
As this answer was referenced by another question and there was a question in the comments, why we need the
MutationObserver
in the first place, I finally did look up the source code of the inputbootstrap-select.js
and my intuition was right, clicks on the.dropdown-header
are actively prevented from bubbling up: