我的闪亮算法(底部的 app.R 代码):
- 要求用户上传文件
- 提供带有选项“无”、“国家”、“州”、“城市”的下拉字段
- 选择“无”时,只应出现文本输出
- 选择“国家/地区”时,应仅显示国家/地区过滤器
- 选择“州”时,应出现国家和州过滤器
- when "City" is selected, both country and city filters should appear and any existing state filter from previous selections should disappear
我在代码中所做的:对“无”和“国家”使用 IF 条件;用于“州”和“城市”的开关。
我的问题: 一切都按预期工作,除了一个:如果我们在选择“None”后选择“State”,我会看到 textOutput 和 State filter 而不是 country 和 state filters。发生这种情况是因为“无”或“国家/地区”中的 IF 语句与 switch() 不同,它只打印 UI,如果进行了另一个选择,则不会清除 UI
我的约束:
国家/地区过滤器输入需要在代码中重复(“州”应该同时提供国家和州过滤器,“城市”类似)但我们不能在闪亮代码中复制具有相同 ID 的输入
我不能使用不同的国家过滤器 ID 来提供相同的输入,因为我需要在多个地方读取国家过滤器中的输入值,并且我需要复制所有这些,这会使一切复杂化
我需要帮助的地方: 我唯一的问题是,如果用户在“无”(绕过“国家”!!!)。
真的需要这个东西来解决。任何帮助,将不胜感激
谢谢!
应用程序.R代码
library(shiny)
ui <- fluidPage(
fileInput("file_attr", "Door attributes:"),
selectInput("select", label = "Region Drop down", choices = list("None", "Country", "State","City"), selected = "None"),
uiOutput("NoneCountryFilter"),
uiOutput("StateCityFilter")
)
server <- function(input, output, session) {
#Reading input
data_attr <- reactive({
file1 <- input$file_attr
if(is.null(file1)){return()}
read.table(file=file1$datapath, sep=",", header = TRUE, stringsAsFactors = FALSE)
})
#Filter interactivity
#Reading Lists
countries <- reactive({
if(is.null(data_attr()$Country)){return()}
data_attr()$Country
})
states <- reactive({
if(is.null(data_attr()$State)){return()}
data_attr()$State[data_attr()$Country %in% input$show_vars]
})
cities <- reactive({
if(is.null(data_attr()$City)){return()}
data_attr()$City[data_attr()$Country %in% input$show_vars]
})
#Filters based on Region Drop down
observeEvent(input$file_attr,{
observe({
if ("None" %in% input$select){
output$NoneCountryFilter <- renderUI({
if(is.null(data_attr()$Country)){return()}
h4("No region Selected")
})
}
if ("Country" %in% input$select){
output$NoneCountryFilter <- renderUI({
if(is.null(data_attr()$Country)){return()}
selectizeInput('show_vars', 'Country Filter', choices = c("Select All","None", unique(countries())), multiple = TRUE)
})
}
})
output$StateCityFilter <- renderUI({
switch(input$select,
"State" = (
selectizeInput('show_vars_state', 'State Filter', choices = c("Select All","None", unique(states())), multiple = TRUE)
),
"City" = (
selectizeInput('show_vars_city', 'City Filter', choices = c("Select All","None", unique(cities())), multiple = TRUE)
)
)
})
})
#Giving "Select ALL" and "None" Functionality to each filter (This part is redundant for the current problem. I am keeping this so that I could check any solution from stackoverflow should not effect other functionalities)
#Countries- SelectAll & None
observe({
if ("Select All" %in% input$show_vars){
selected_choices <- setdiff(c("Select All",unique(countries())), "Select All")
updateSelectizeInput(session, 'show_vars', choices = c("Select All","None",unique(countries())), selected = selected_choices)
}
})
observe({
if ("None" %in% input$show_vars){
updateSelectizeInput(session, 'show_vars', choices = c("Select All", "None", unique(countries())),selected = "")
}
})
#State- SelectAll & None
observe({
if ("Select All" %in% input$show_vars_state){
selected_choices <- setdiff(c("Select All",unique(states())), "Select All")
updateSelectizeInput(session, 'show_vars_state', choices = c("Select All","None",unique(states())), selected = selected_choices)
}
})
observe({
if ("None" %in% input$show_vars_state){
updateSelectizeInput(session, 'show_vars_state', choices = c("Select All", "None", unique(states())),selected = "")
}
})
#City- SelectAll & None
observe({
if ("Select All" %in% input$show_vars_city){
selected_choices <- setdiff(c("Select All",unique(cities())), "Select All")
updateSelectizeInput(session, 'show_vars_city', choices = c("Select All", "None", unique(cities())), selected = selected_choices)
}
})
observe({
if ("None" %in% input$show_vars_city){
updateSelectizeInput(session, 'show_vars_city', choices = c("Select All", "None", unique(cities())),selected = "")
}
})
}
shinyApp(ui = ui, server = server)