Condition Filter in dplyr based on Shiny input

前端 未结 2 980
感动是毒
感动是毒 2020-12-17 06:31

I\'m creating a reporting tool for an online market. I want to add a checkbox, \"Coupon\", where only observations that have a positive value in the Coupon field are selecte

2条回答
  •  醉酒成梦
    2020-12-17 07:03

    If you want to stick with dplyr you can include an if else statement in your filtering statement for Orders like this:

    Orders %>% 
    filter(Region %in% Region_Select(), Community.Type %in% Type_Select() %>% 
    { if (input$coupon == TRUE) filter(., Coupon > 1) else filter(., Coupon > -1 | is.na(Coupon)) }
    

    The first filter is for the items you want to filter regardless of the Coupon checkbox. Inside the curly brackets is an if statement that will filter as you have specified if the checkbox is marked and if not, it will keep everything if I understand the possible outcomes.

    I use this type of setup for creating interactive tables in a shiny app.

提交回复
热议问题