我的闪亮算法(app.R 代码在底部):
- 要求用户上传文件
- 提供带有选项“无”、“国家”、“州”、“城市”的下拉字段
- 选择“无”时,仅应出现文本输出
- 选择“国家/地区”时,仅应显示国家/地区过滤器
- 选择“州”时,应同时显示国家/地区和州过滤器
- 选择“城市”时,国家和城市过滤器都应该出现,并且之前选择的任何现有州过滤器都应该消失
我在代码中做了什么:
使用 IF 条件表示“无”和“国家/地区”;使用“州”和“城市”的开关。
我的问题:一切都按预期工作,除了:如果我们在选择“无”后选择“州”,我会看到文本输出和州过滤器,而不是国家和州过滤器。发生这种情况是因为“None”或“Country”中的 IF 语句与 switch() 不同,仅打印 UI,并且如果进行其他选择则不会清除 UI
我的约束:
我需要帮助的地方:我唯一的问题是,如果用户在“无”之后选择(“州”/“城市”)(绕过“国家”),我需要同时显示“国家”和(“州”/“城市”)过滤器! )。
确实需要修复这个东西。任何帮助,将不胜感激
Thanks!
虚拟数据:https://docs.google.com/spreadsheets/d/1E9dbtOMm1-7ZjIHu3Ra_NyFBHrCQdITG2_xuMIMKDOs/edit?usp=sharing https://docs.google.com/spreadsheets/d/1E9dbtOMm1-7ZjIHu3Ra_NyFBHrCQdITG2_xuMIMKDOs/edit?usp=sharing
应用程序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)