我的闪亮算法(底部是app.R代码):
我在代码中做了什么: 使用IF条件为"无"和"国家&#34 ;;用于"状态"和"城市"。
我的问题: 一切都按预期工作,除了一个:如果我们选择" State"选择"无"后,我看到textOutput和State过滤器而不是国家和州过滤器。发生这种情况是因为IF中的IF语句没有"或者" Country",与switch()不同,只是打印UI并且如果做出其他选择则不会清除UI
我的约束:
国家过滤器输入需要在代码中重复(" State" 应该给国家和州过滤器,类似于" City")但是 我们无法在闪亮的代码中复制具有相同ID的输入
我无法使用不同的国家/地区过滤器ID来提供相同的输入 因为我需要读入国家过滤器中的输入值 多个地方,我需要复制所有那些和那 会使一切复杂化
我需要帮助的地方: 我唯一的问题是,我需要"国家"和("状态" /"城市")过滤器出现,如果用户选择("状态" /"城市")&#34 ;无" (绕过"国家" !!!)。
真的需要这个东西来修复。任何帮助将不胜感激
谢谢!
虚拟数据:https://docs.google.com/spreadsheets/d/1E9dbtOMm1-7ZjIHu3Ra_NyFBHrCQdITG2_xuMIMKDOs/edit?usp=sharing
app.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)
答案 0 :(得分:0)
您可以使用conditionalPanel
根据特定条件确定要显示的元素。
注意:
我在req()
中使用data_attr
,因为这是对file_attr
的检查。您不必使用is.null()
。
我在国家,州,城市使用了三个conditionalPanel
,并为每个人指定了条件。
在条件中,使用的input.select
不是input$select
。
library(shiny)
ui <- fluidPage(
fileInput("file_attr", "Door attributes:"),
selectInput("select", label = "Region Drop down",
choices = list("None", "Country", "State","City"), selected = "None"),
# Selectize Inputs ---------------
uiOutput("Country_List"),
uiOutput("State_List"),
uiOutput("City_List")
)
server <- function(input, output, session) {
#Reading input
data_attr <- reactive({
req(input$file_attr) # make sure a file was uploaded
read.table(file=input$file_attr$datapath, sep=",", header = TRUE, stringsAsFactors = FALSE)
})
# Country selectizeInput ----------------------
output$Country_List <- renderUI({
conditionalPanel(
condition = "input.select == 'Country' | input.select == 'State'| input.select == 'City' ",
selectizeInput('show_var', 'Country Filter',
choices = c("Select All","None", unique(data_attr()$Country)), multiple = TRUE)
)
})
# State selectizeInput ----------------------
output$State_List <- renderUI({
conditionalPanel(
condition = "input.select == 'State' ",
selectizeInput('show_vars_state', 'State Filter',
choices = c("Select All","None", unique(data_attr()$State)), multiple = TRUE)
)
})
# City selectizeInput -----------------------
output$City_List <- renderUI({
conditionalPanel(
condition = "input.select == 'City' ",
selectizeInput('show_vars_city', 'City Filter',
choices = c("Select All","None", unique(data_attr()$City)), multiple = TRUE)
)
})
}
shinyApp(ui = ui, server = server)