r - 如何将变量和“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..."
知道这是为什么吗?
解决方案
UI 脚本如下所示:
# SelectInput Query
selectInput(inputId = "S1",label = "Select one of the choices",
choices = c("All",unique(dataframe()$column)))
这里默认选择 selectInput 的第一个值为“All”。选择是全部以及数据框中所需的任何列的唯一值。
推荐阅读
- python - 我无法安装scrapy
- sockets - 在本地测试 UDP 广播
- php - 将 XML 解析为值可能不存在的表
- python - 有没有办法从python中的字符串中找到关键字
- c# - Formview Image Control 在模式切换时清除代码设置的 imageURL
- eclipse - Eclipse 给了我无效的参数“候选人是:float atanf(float)”,即使参数是有效的
- javascript - 使用 Jquery 限制带有父 html 选择器的复选框选择
- scipy - 如何使用 SciPy 重用 Jacobian 和 Inverse Hessian 最小化
- html - 如何在引导程序中在我的屏幕上均匀分布五列?
- python - python,linux:如何将整个硬盘驱动器作为一个原始二进制文件打开