Home > Mobile >  Why the selectInput() does not work correctly when multipe = TRUE?
Why the selectInput() does not work correctly when multipe = TRUE?

Time:12-23

I want to get the items that were selected from selectInput(). However, it seems only the first item could be transferred to server(). The UI:

      sidebarLayout(
        sidebarPanel(width = 4,
          selectInput("cell_marker_density_surv", "Cell type", choices = cell_list, 
                      selected = colnames(density)[1:3], multiple = TRUE),
          textOutput("warning_density_roc_1"),
        ),
        mainPanel(width = 8,
        )
      )

The server()

warning_density_roc_1_output <- reactive({
  a = input$cell_marker_density_surv
  paste0(input$cell_marker_density_surv, collapse = ",")
})

output$warning_density_roc_1 <- renderText(warning_density_roc_1_output())

As we can see, only the first item showed, even in the default situation.

enter image description here

Old answer:

Maybe this can help, I used mpg dataset as dummy data.

library(shiny)

mpg <- ggplot2::mpg

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      width = 4,
      textOutput("warning_density_roc_1"),
      selectInput("cell_marker_density_surv", "Cell type",
        choices = names(mpg),
        selected = names(mpg)[1:6], multiple = TRUE
      )
    ),
    mainPanel(width = 8, )
  )
)

server <- function(input, output, session) {
  warning_density_roc_1_output <- reactive({
    if (length(input$cell_marker_density_surv) > 5) {
      "Warning, more than five items selected."
    }
  })

  output$warning_density_roc_1 <- renderText(warning_density_roc_1_output())
}

shinyApp(ui, server)
  • Related