rstudio / shiny

Easy interactive web applications with R
https://shiny.posit.co/
Other
5.37k stars 1.87k forks source link

selectInput multiple, ADD: replace/duplicate argument #518

Open jangorecki opened 10 years ago

jangorecki commented 10 years ago

Hi, Would be nice to allow multiple selections of the same item in the selectInput. Consider we have choices = c("field1","field2","field3","field4") and we want to pass from this input to shinyServer a vector of c("field1","field2","field2"). Currently AFAIK you cannot do that. Regards, Jan

yihui commented 10 years ago

You can use the "vanilla" select input, i.e. selectInput(..., selectize = FALSE, multiple = TRUE), since selectize.js (@brianreavis) does not support duplicated values yet:

Whenever selectize.js supports this feature, we will try to catch up. Thanks!

jangorecki commented 10 years ago

Yihui, could you please re-open issue? I'm not able to achieve it using selectInput non selectize. You may not understand my question well, it is related to duplicates not in the choices vector but in the output vector. Please see example below

library(shiny)
runApp(list(
  ui = bootstrapPage( 
    sidebarPanel(
      selectInput("select1", "Select1:", c("A", "B", "C"), selectize = FALSE, multiple = TRUE)
    ),
    mainPanel(
      verbatimTextOutput("val")
      )
  ),
  server = function(input, output) {
    output$val <- renderPrint(input$select1)
  }
))

I would like to print: "A" "B" "B" which means no duplicates in choices arg but still duplicates on the selection/results. Regards

yihui commented 10 years ago

I see. I do not think that is possible unless you provide A, B, B, C as the choices. This comes from the limitation of the web browser/standard, and we cannot do much about it in shiny. Sorry.

debsush commented 8 years ago

Selectize now enables duplicate entries. Can we have that feature in Shiny now

fabiangehring commented 6 years ago

Just came accross this older issue. I‘m having the same problem atvthe moment. @wch or @jcheng5 any chance to reopen this?

jcheng5 commented 5 years ago

It doesn't look to me like it is supported in selectize, fwiw.

alandipert commented 5 years ago

I also didn't see this anyhere in Selectize, but I did see the feature request in a couple places (https://github.com/selectize/selectize.js/issues/129, https://github.com/selectize/selectize.js/issues/1371)

@debsush do you remember where you saw that it was supported by any chance?

Anyway, I offer the following workaround, which relies on renderUI:

ezgif-5-5aa6bc79f816

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      uiOutput("selectContainer")
    ),
    mainPanel(
      verbatimTextOutput("val"),
      actionButton("reset", "Reset")
    )
  )
)

server <- function(input, output, session) {
  choices <- c("A", "B", "C")
  chosen <- reactiveVal(c())
  observeEvent(input$reset, chosen(c()))
  output$selectContainer <- renderUI({
    # Take a dependency on chosen to re-render when an option is selected.
    chosen()
    selectInput( "select1", "Select1:", choices, selectize = TRUE, multiple = TRUE)
  })
  observeEvent(input$select1, {
    chosen(c(chosen(), input$select1))
  })
  output$val <- renderPrint(chosen())
}

shinyApp(ui, server)