【问题标题】:为什么当 multiple = TRUE 时 selectInput() 不能正常工作?
【发布时间】:2022-01-23 16:40:32
【问题描述】:

我想获取从 selectInput() 中选择的项目。但是,似乎只有第一项可以转移到 server()。 用户界面:

      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() 无法正常工作。

【问题讨论】:

    标签: r shiny shiny-server shinyjs selectinput


    【解决方案1】:

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

    【讨论】:

    • 非常感谢您的关心!在您的帮助下,我已经解决了这个问题!我想使用 feedbackWarning() 来回馈警告。但是,根据您修改的代码不起作用。我已经测试并发现 if() 可以正常工作。我不确定问题是否是由 feedbackWarning() 引起的。你能帮我测试一下吗?谢谢!
    • 题中根据你修改的代码已更新。
    • @tuanzi 我更新了我的答案以添加feedbackWarning() case。
    • 感谢您的帮助!问题已经解决了!
    猜你喜欢
    • 2014-12-15
    • 2018-08-07
    • 2016-07-16
    • 2019-01-04
    • 2020-09-03
    • 2016-10-10
    • 2016-10-24
    • 2017-02-27
    • 2017-07-08
    相关资源
    最近更新 更多