How can I arrange the order of the selected for the multiInput option in the order of selection?

Hello,
The row of items selected in the shiny app below is "Banana", "Coconut", "Kiwi", "Lemon", "Lime", "Mango", "Grapefruit","Orange", "Papaya", "Blueberry", "Cherry" I want to select it and I want it to be in this order on the selected screen, which I have taken the screenshot below. Can you help me with this?

Thank you so much.

if (interactive()) {

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

simple use

ui <- fluidPage(
multiInput(
inputId = "id", label = "Fruits :",
choices = c("Banana", "Blueberry", "Cherry",
"Coconut", "Grapefruit", "Kiwi",
"Lemon", "Lime", "Mango", "Orange",
"Papaya"),
selected = "Banana", width = "350px"
),
verbatimTextOutput(outputId = "res")
)

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

shinyApp(ui = ui, server = server)

with options

ui <- fluidPage(
multiInput(
inputId = "id", label = "Fruits :",
choices = c("Banana", "Blueberry", "Cherry",
"Coconut", "Grapefruit", "Kiwi",
"Lemon", "Lime", "Mango", "Orange",
"Papaya"),
selected = "Banana", width = "400px",
options = list(
enable_search = FALSE,
non_selected_header = "Choose between:",
selected_header = "You have selected:"
)
),
verbatimTextOutput(outputId = "res")
)

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

shinyApp(ui = ui, server = server)

}
multiInput

So set the choices in the way you desire

The order of the options selected from the data set is important for my study. So, ranking banana-strawberry-kiwi versus kiwi-banana-strawberry changes my study results. So your suggestion does not serve my purpose.

Below is an example app that captures the order of the selections in a reactiveValue(), as shown by the second list of printed inputs in the screenshot. I tried passing this ordered list of selections to the input via updateMultiInput(), but it is put back in alphabetical order when displayed. So, I'm not sure how to make it show in proper order in the input, but if you need to capture the selections, this could serve as a potential alternative.

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

ui <- fluidPage(
  multiInput(
    inputId = "id", 
    label = "Fruits :",
    choices = c("Banana", "Blueberry", "Cherry",
                "Coconut", "Grapefruit", "Kiwi",
                "Lemon", "Lime", "Mango", "Orange",
                "Papaya"),
    selected = "Banana", 
    width = "600px",
    options = list(
      enable_search = FALSE,
      non_selected_header = "Choose between:",
      selected_header = "You have selected:"
    )
  ),
  verbatimTextOutput(outputId = "res"),
  HTML('Below is the order captured in the reactiveValue'),
  verbatimTextOutput(outputId = "rx")
)

server <- function(input, output, session) {
  
  selections = reactiveValues(d = NULL)
  
  observeEvent(input$id, {
    if(is.null(selections$d)) {
      out = input$id
    } else {
      new = setdiff(input$id, selections$d)
      out = c(selections$d, new)
    }
    selections$d <<- out
    
    updateMultiInput(session,
                     'id',
                     selected = selections$d)
  })

  output$res <- renderPrint({
    input$id
  })
  
  output$rx = renderPrint({
    selections$d
    })
}

shinyApp(ui = ui, server = server)

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.