R Shiny pickerInput select all text

shiny
htmlwidget

#1

Using shinywidgets pickerinput I am trying to use a dropdown. Below is the code. Could someone tell me how to display the text total in the display input when then user selects all inputs.I can show the count but not the text total instead.

library("shiny")
library("shinyWidgets")

ui <- fluidPage(
  column(
    width = 4,
    pickerInput(
      inputId = "id", label = "Choices :",
      choices = c("Banana", "Blueberry", "Cherry", "Coconut", "Grapefruit",
                  "Kiwi", "Lemon", "Lime", "Mango", "Orange", "Papaya"),
      options = list(`actions-box` = TRUE, `selected-text-format` = "count > 2",
                     `count-selected-text` = "{0}/{1} fruits"),
      multiple = TRUE
    )
  )
)


server <- function(input, output) {
  output$res <- renderPrint({
    input$id
  })
}

shinyApp(ui = ui, server = server)

#2

Hi, can you elaborate what you mean by "text total"? I can't tell if you want to display the total number of selected items in the pickerInput, or if you want the title of every selected choice to appear, or something else. Thanks for clarifying.


#3

O0x3c

@alandipert Instead of "Total:11/11 fruits selected "I just want to show the text "Total" when all options are selected.

Thank you.


#4

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.