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

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

问题描述

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

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.

所以,在 ui.R 中我有:

So, in ui.R I have:

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

这工作正常.

在 server.R 中,我有:

In server.R I have:

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

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

这里的想法是,如果选中复选框,dplyr 将仅选择优惠券"值 > 0 的观察值.如果未选中,它将选择优惠券"值 > -1 的观察值.但是,我现在意识到它不起作用,因为没有价值的优惠券被赋予了 NA - 因此,无论复选框的值如何,我只会得到优惠券价值 > 0 的观察结果.

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.

所以,我的问题是,如何使 dplyr 仅输出带有 Coupon 值的观察值,当复选框被选中时不是 NA,而当它没有被选中时所有观察值?

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?

推荐答案

鉴于您指出实际存在多个变量需要过滤 NA 或不过滤,您可以使用标准执行此操作通过 filter_ 进行评估和包 lazyeval 的一些帮助.

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)

算法应该是这样的:

首先,对于您想要删除缺失值或保留它们的每个复选框,您将在 server.r 中做出反应性声明,类似于您的问题,除了它将返回 NULL 或您用作字符串的数据集中的变量名称.

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

您将在 Data_Select 反应性函数中使用这些反应性函数的输出.在这一步中,您将把复选框反应性结果连接到一个向量或列表中,然后使用 lapply 循环遍历它们,为 filter_ 中使用的每个变量设置条件代码>.这涉及使用包 lazyeval 中的 interp,就像在这个答案.请注意,这在对每个变量使用相同条件时有效(删除包含这些特定变量缺失值的行).

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

要过滤的条件的输出列表可以在 filter_.dots 参数中使用.我添加了 filter_ 作为第二个过滤步骤,因此您仍然可以通过 filter 轻松完成其他条件.

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

我发现当所有复选框反应性函数都返回 NULL 并且您不需要任何额外的过滤时,这特别有用.

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

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

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