【问题标题】:Filtering data with condition on users' input in a Shiny app根据用户在 Shiny 应用程序中的输入条件过滤数据
【发布时间】: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)%&gt;% distinct(REGION), "All")

标签: r filter shiny dplyr


【解决方案1】:

我终于找到了一个分配反应条件的解决方案,然后在过滤语句中对其进行评估。这是我的代码:

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)
  )
}

target_pop <- function(input, output, session){

  #update of region propositions depending on chosen country
  observeEvent(input$input1,{
    updateSelectInput(session = session, inputId = "input2", label = "Region", choices = c(get(input$input1)%>%
                        distinct(REGION), "All"))

  })


  cond1 <- reactive({if (input$input2 != "All") quote(REGION == input$input2) else TRUE})

  #Update of "sex propositions" depending on chosen region and country
  observeEvent(input$input2,{

    updateSelectInput(session = session, inputId = "input3", label = "Sex", choices = 
                        c(get(input$input1)%>%
                          filter(!!cond1())%>%
                          left_join(sex_labels)%>%
                          distinct(SEX_LABELS)%>%
                          rename(Sex = SEX_LABELS), "All"))
  })

【讨论】:

    猜你喜欢
    • 2017-04-09
    • 1970-01-01
    • 1970-01-01
    • 2018-07-31
    • 2018-11-05
    • 1970-01-01
    • 2017-03-16
    • 2016-11-19
    • 1970-01-01
    相关资源
    最近更新 更多