如何从变量和“添加唯一值”;“全部”;到Shining slector中的下拉菜单
我使用了一个输入(csv)文件来填充shiny中的数据框,我计划做的是创建一个选项列表,其中包含变量中的所有唯一值,以及一个“all”选项,这样默认情况下,选定的值为all,它不会过滤数据集,但如果选择了任何其他值,它将相应地过滤数据集。部分代码如下所示:如何从变量和“添加唯一值”;“全部”;到Shining slector中的下拉菜单,r,shiny,shinydashboard,R,Shiny,Shinydashboard,我使用了一个输入(csv)文件来填充shiny中的数据框,我计划做的是创建一个选项列表,其中包含变量中的所有唯一值,以及一个“all”选项,这样默认情况下,选定的值为all,它不会过滤数据集,但如果选择了任何其他值,它将相应地过滤数据集。部分代码如下所示: data_set <- reactive({ req(input$file1) inFile <- input$file1 data_set <-read.csv(inFile$datapath, header=
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,1,2,3,4,…”,而不是预期值“ALL,VAL1,VAL2,VAL3…”
你知道为什么吗???用户界面脚本会是这样的:
# SelectInput Query
selectInput(inputId = "S1",label = "Select one of the choices",
choices = c("All",unique(dataframe()$column)))
在这里,默认情况下将选择selectInput的第一个值为“All”。这些选项都是数据框中所需的所有列的唯一值。我想知道它是
更新选择输入
还是选择输入
??因为,我想,被动输入不会按照您的建议包含所有内容。相反,我使用了choices=c(“ALL”,unique(data_set()$variable))
和updateSelectInput(session,“Var1”,label=“Select ID”,choices=choices,selected=choices[1])
解决了这个问题。但我在下拉列表中得到的值是,1,2,3,4,。。。而不是全部,VAL1,VAL2,VAL3。。。知道为什么吗?在服务器端试试这个:updateSelectInput(会话,“Var1”,selected=“All”)
我修改了代码,它成功了choices=c(“ALL”,unique(data_set()$variable))
是我应该做的。
updateSelectInput(session,"Var1", label = "Select ID", choices = choices, selected=choices[1])
# SelectInput Query
selectInput(inputId = "S1",label = "Select one of the choices",
choices = c("All",unique(dataframe()$column)))