OK thanks for clarifying. Here is a near-solution:
library("shiny")
library("shinyWidgets")
fruits <- c("Banana", "Blueberry", "Cherry", "Coconut", "Grapefruit", "Kiwi", "Lemon", "Lime", "Mango", "Orange", "Papaya")
pickerMaker <- function(selected = c(), selectedText = "{0}/{1} fruits") {
pickerInput(
inputId = "picker",
label = "Choices :",
choices = fruits,
selected = selected,
options = list(
`actions-box` = TRUE,
`selected-text-format` = "count > 2",
`count-selected-text` = selectedText
),
multiple = TRUE
)
}
ui <- fluidPage(
titlePanel("Fruit Explorer"),
sidebarLayout(sidebarPanel(uiOutput("picker")),
mainPanel(textOutput("res")))
)
server <- function(input, output) {
nTotal <- length(fruits)
nPrevSelected <- reactiveVal(0)
nSelected <- reactive(length(input$picker))
observe(nPrevSelected(length(input$picker)))
pickerTransition <- reactive({
if (nPrevSelected() < nTotal && nSelected() == nTotal) {
"toTotal"
} else if (nPrevSelected() == nTotal && nSelected() < nTotal) {
"fromTotal"
} else if (nSelected() > 0) {
"noop"
} else {
"init"
}
})
output$picker <- renderUI({
switch(pickerTransition(),
toTotal = pickerMaker(input$picker, "Total"),
fromTotal = pickerMaker(input$picker),
noop = req(FALSE, cancelOutput = TRUE),
init = pickerMaker())
})
output$res <- renderPrint({
input$picker
})
}
shinyApp(ui = ui, server = server)
The reason it's a "near-solution" and not a full solution is that during the transition from total-1 being selected to total being selected, the selection dropdown disappears because renderUI is used to replace the pickerInput instance with one that has a different count-selected-text parameter.
The same problem happens during the transition from total to total-1. Anyway, I post it because I figure it might be good enough for you.
An ideal solution is maybe an addition to shinyWidgets like an updatePicker function, that gives one the ability to change picker parameters without destroying the picker. This would be analogous to shiny::updateTextInput.