【发布时间】:2019-11-08 02:56:34
【问题描述】:
我正在编写一个闪亮的模块来根据用户的输入过滤我的数据。基本上如果用户选择了一个国家和一个地区,程序就会选择这个国家对应的数据集并过滤特定的地区。过滤特定区域后,它会过滤性别。
# ui part
target_pop_UI <- function(id){
ns <- NS(id)
tagList(
selectInput(inputId = ns("input1"), label = "Country", choices = c("ITA", "BFA", "ZMB")),
selectInput(inputId = ns("input2"), label = "Region", choices = NULL),
selectInput(inputId = ns("input3"), label = "Sex", choices = NULL)
)
}
# server part
target_pop <- function(input, output, session){
observeEvent(input$input1,{
updateSelectInput(session = session, inputId = "input2", label = "Region", choices = c(get(input$input1)%>%
distinct(REGION), "All")
})
observeEvent(input$input2,{
updateSelectInput(session = session, inputId = "input3", label = "Sex", choices = c(get(input$input1)%>%
filter(if(input$input2 != "All") {REGION == input$input2})%>%
distinct(SEX_LABELS)
)}
}
如果用户选择特定区域,这将起作用。但是如果他选择“全部”,我不想过滤数据集。
我尝试了不同的选项(在第二个 observeEvent 中)但它不起作用:
filter(if(input$input2 != "All") {REGION == input$input2})
#or
filter(if(input$input2 != "All") {REGION == input$input2} else{})
#or
filter(if(input$input2 != "All") {REGION == input$input2} else{NULL})
显示的错误是:
Error in : Argument 2 filter condition does not evaluate to a logical vector
总而言之,我想要过滤如果 Region != "All" 然后过滤,否则不做任何事情。
如果有人可以帮助我,谢谢。
【问题讨论】:
-
您的区域选择似乎是
choices = NULL,但我假设情况并非如此:您可以尝试filter(ifelse(input$input2 %in% [the choice statement in your input2],REGION == input$input2, TRUE))。 -
这些选项在服务器部分更新:
updateSelectInput(session = session, inputId = "input2", label = "Region", choices = c(get(input$input1)%>% distinct(REGION), "All")