基于Shiny输入的条件过滤器 [英] Condition Filter in dplyr based on Shiny input

查看:191
本文介绍了基于Shiny输入的条件过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为在线市场创建一个报告工具。我想添加一个复选框优惠券,其中只有在优惠券字段中具有正值的观察结果被选中。



所以在ui.RI中有: / p>

  checkboxInput(checkbox,label =Coupon,value = TRUE)

这是正常的。



在server.RI中有:

  Coupon_Select<  -  reactive({
if(input $ checkbox == TRUE){0}
else {-1}
})

  Data_Select<  -  reactive({
Orders%>%
filter(%Region_Select()中的Region%,%Type_Select()中的Community.Type%
优惠券> Coupon_Select()

})

这里的想法是,如果选中复选框,则dplyr将仅选择Coupon值为0的观察值。如果未选中,则会选择Coupon值> -1的观察值。然而,我意识到现在它不工作,因为没有价值的优惠券被给予NA - 因此,无论复选框的价值如何,我只收到观察值,优惠券值> 0。



所以,我的问题是,如果在选中复选框时,怎样才能使dplyr输出仅使用不为NA的优惠券值的观察结果,以及当没有选中时的所有观察结果?

解决方案

鉴于您表示有实际的多个变量,您需要过滤 NA 您可以通过 filter _ 的标准评估,并从package lazyeval 中获得一些帮助。

 库(dplyr)
库(lazyeval)

该算法将是这样的:



首先,对于每个复选框,您要删除缺少的值或保留它们,您将使 server.r 中的被动语句类似于您的问题,除了它将返回 NULL 或您用作字符串的数据集中的变量名。

  Coupon_Select<  -  reactive({
if(input $ checkbox){Coupon}
else {NULL}
})

Sale_Select< - reactive({
if(input $ checkbox2){Sale}
else {NULL}
})

您将在 Data_Select 中使用这些反应函数的输出功能。在此步骤中,您可以将复选框反应结果连接到一个向量或列表中,然后用 lapply 循环,以设置每个变量的条件过滤器_ 。这涉及到使用包 lazyeval 中的 interp ,就像这个答案。请注意,对每个变量使用相同的条件(删除包含这些特定变量的缺失值的行)时,这可以工作。



过滤条件的输出列表可以是用于 .dot 参数 filter _ 。我添加了 filter _ 作为第二个过滤步骤,所以你总是拥有的其他条件仍然可以通过过滤器

  dataInput = reactive({
extrafilt = c(Coupon_Select(),Sale_Select())
dots = lapply(extrafilt,function(cols)interp(〜!is.na(x),
.values = list(x = as.name(cols)))
订单%> ;%
过滤器(%Region_Select()中的Region%,%Type_Select()中的Community.Type%)%>%
过滤器_(。dots =点)
})

我发现特别有用的是,当所有复选框反应函数返回 NULL ,您不需要任何额外的过滤。


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 selected.

So, in ui.R I have:

checkboxInput("checkbox", label = "Coupon", value = TRUE)

This is working fine.

In server.R I have:

  Coupon_Select <- reactive({ 
    if(input$checkbox == TRUE){0}
      else {-1}  
  })

and

Data_Select <- reactive({
    Orders %>%
      filter(Region %in% Region_Select(), Community.Type %in% Type_Select(), 
             Coupon > Coupon_Select()
      )
    })

The idea here is that if the Checkbox is checked, dplyr would only select observations whose 'Coupon' value > 0. If it's not checked, it would select observations whose 'Coupon' value > -1. However, I realize now it doesn't work because Coupons with no value are given a NA - therefore, regardless of the value of the checkbox, I'm only getting observations with coupon values > 0.

So, my question is, how can I make dplyr output only observations with Coupon values that aren't NA when the checkbox is checked, and all observations when it's not checked?

解决方案

Given that you indicated that there are actual multiple variables you need to either filter for NA or not, you could do this using standard evaluation via filter_ and some help from package lazyeval.

library(dplyr)
library(lazyeval)

The algorithm would be something like this:

First, for each of your check boxes that you want to either remove the missing values or keep them, you would make a reactive statement in server.r kind of like in your question, except it would either return NULL or the variable name from the dataset you are using as a string.

Coupon_Select <- reactive({ 
    if(input$checkbox){"Coupon"}
      else {NULL}  
  })

Sale_Select <- reactive({ 
    if(input$checkbox2){"Sale"}
      else {NULL}  
  })

You'll use the output of these reactive functions in your Data_Select reactive function. In this step, you'll concatenate the check box reactive result together into a vector or list and then loop through them with lapply to set up the condition for each variable for use in filter_. This involves using interp from package lazyeval much as in this answer. Note that this works when using the same condition for each variable (removing rows that contain missing values for those particular variables).

The output list of conditions to filter on can be used in the .dots argument of filter_. I added the filter_ as a second filtering step, so the other conditions that you will always have can still easily be done via filter.

dataInput = reactive({
        extrafilt = c(Coupon_Select(), Sale_Select())
        dots = lapply(extrafilt, function(cols) interp(~!is.na(x), 
                                                        .values = list(x = as.name(cols))))
        Orders %>%
            filter(Region %in% Region_Select(), Community.Type %in% Type_Select())  %>%
            filter_(.dots = dots)
    })

I found it particularly useful that this works when all check box reactive functions return NULL and you don't need any additional filtering.

这篇关于基于Shiny输入的条件过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆