首页 > 解决方案 > 如何将变量和“ALL”中的唯一值添加到闪亮选择器的下拉菜单中

问题描述

我使用了一个输入(csv)文件来填充一个闪亮的数据框,我打算做的是创建一个选项列表,其中包含来自变量的所有唯一值以及“ALL”选项,以便通过默认选择的值为ALL,它不会过滤数据集,但如果选择任何其他值,它将相应地过滤数据集。部分代码如下所示:

data_set <- reactive({
  req(input$file1)
  inFile <- input$file1
  data_set <-read.csv(inFile$datapath, header=input$header)
})

observe({
    require(dplyr)
    req(input$file1)    # the dataset is read from CSV file
    choices = c("ALL", unique(as.character(data_set()$variable)))

    updateSelectInput(session,"Var1",
                             label = "Select ID",
                             choices = choices, selected=choices[1])

})

selectInput(inputId = "Var1", label = "Select ID", multiple = FALSE, 
             choices = list("ALL") )


df1 <- reactive({
      if input$Var1 == "ALL" {
        data_set()  
      } else {
        data_set() %>%
          filter(variable == input$Var1)
      }

  })

我无法在代码中包含“ALL”选项,我需要这方面的帮助。

感谢 Praneeth(下)的想法,我修改了我的代码以解决上述问题:

 choices = c("ALL", unique(data_set()$variable)) 

 updateSelectInput(session,"Var1", label = "Select ID", choices = choices, selected=choices[1]) 

这解决了这个问题。

但是我在下拉列表中得到值"ALL, 1,2,3,4,..."而不是期望值"ALL, VAL1, VAL2, VAL3..."

知道这是为什么吗?

标签: rshinyshinydashboard

解决方案


UI 脚本如下所示:

# SelectInput Query

selectInput(inputId = "S1",label = "Select one of the choices",
               choices = c("All",unique(dataframe()$column)))

这里默认选择 selectInput 的第一个值为“All”。选择是全部以及数据框中所需的任何列的唯一值。


推荐阅读