为什么 selectInput() 在 multiple = TRUE 时不能正常工作?

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

我想获取从 selectInput() 中选择的项目。但是,似乎只有第一项可以传输到 server()。 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,
        )
      )

服务器()

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())

我们可以看到,即使在默认情况下,也只显示第一项。

enter image description here

我意识到与这些问题相关的问题很多,但我不知道如何解决。是selectInput()函数本身引起的吗?事实上,我想在选择的输入超过五个时给出警告,所以我需要知道选择了多少个项目。你可以帮帮我吗?谢谢!

以下是根据第一个答案修改后的代码:

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)

但是,feedbackWarning() 无法正常工作。

编辑:使用 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)

旧答案:

也许这会有所帮助,我使用 mpg 数据集作为虚拟数据。

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)