Search code examples
rshinyshiny-servershinyjsselectinput

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


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

I have realized that there are many questions related to these problems, but I do not know how to solve it. Is it caused by the selectInput() function itself? In fact, I want to give back a warning when the selected inputs are more than five, so I need to know how many items were selected. Could you help me? Thank you!

The following is the code modified based on the first answers:

library(shiny)

mpg <- ggplot2::mpg
library(shinyFeedback)

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({
    print(length(input$cell_marker_density_surv))
    num_input_density_roc <- if(length(input$cell_marker_density_surv) > 5) {
      print("TRUE")
      TRUE
    } else {
      print("FALSE")
      FALSE
    }
    num_input_density_roc
    feedbackWarning("cell_marker_density_surv", num_input_density_roc, "Warning, more than five items selected.")

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

shinyApp(ui, server)

However, the feedbackWarning() could not work correctly.


Solution

  • Edit: Using shinyFeedback::feedbackWarning().

    library(shiny)
    library(shinyFeedback)
    
    mpg <- ggplot2::mpg
    
    ui <- fluidPage(
      shinyFeedback::useShinyFeedback(),
      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) {
      observeEvent(input$cell_marker_density_surv, {
        shinyFeedback::feedbackWarning(
          "cell_marker_density_surv",
          length(input$cell_marker_density_surv) > 5,
          "Warning, more than five items selected."
        )
      })
    }
    
    shinyApp(ui, server)
    

    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)