dplyr组由于不在Shiny中工作 [英] dplyr group by not working in Shiny

查看:178
本文介绍了dplyr组由于不在Shiny中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用R闪亮的应用程序,因为我有两个下拉框。第一个下拉列表中填入分类变量,第二个填充数值变量。然后我在分类变量上应用groupby。
这是我的代码。

I am working with R shiny application,in that i have two dropdown boxes. first drop down is populated with categorical variables and second is populated with numerical variables. And then I am applying groupby on categorical variable. Here is my code.

dataset<- dataUpload()

var1 <- as.character(input$variable1)
var2 <- as.character(input$variable2)

v$data <- dataset %>%
  group_by(dataset[,var1]) %>%
  summarize(Sum=sum(dataset[,var2])) %>%
  arrange(desc(Sum))

它给了我下面的输出。

And it gives me following output.

 Source: local data frame [7 x 2]

  dataset[[var1]]                Sum
           (fctr)               (int)

1     Chicken Biryani            37
2       Chicken Kabab            37
3       Chicken Kadai            37
4         Dal Makhani            37
5 Sai Bhaji and Pulav            37
6          Tava Pulav            37
7          Total Meal            37

这是dish_quantity变量的总和。但我想要这样的东西。

Which is the total sum of dish_quantity variable. But I want something like this.

dish_quant <- df_final %>%
              group_by(dish_name) %>%
              summarize(Dish_Quantity=sum(dish_quantity)) %>%
              arrange(desc(Dish_Quantity))

        dish_name           Dish_Quantity
           (fctr)              (int)
 1       Chicken Kadai            11
 2     Chicken Biryani             9
 3         Dal Makhani             6
 4 Sai Bhaji and Pulav             3
 5          Tava Pulav             3
 6          Total Meal             3
 7       Chicken Kabab             2

我在哪里做错了?我认为在Shiny中引用数据框的列名是有问题的。

Where am I doing wrong? I think there's a problem with referencing column name of a dataframe while doing it in Shiny.

推荐答案

您遇到 SE / NSE dplyr 会做一些不同的事情,通常你用它的名字来调用它,它会对它进行一些处理。但是,当将它与一个字符变量一起使用时,该魔法将失败,并且您需要回退标准评估。

You are running into problems with SE/NSE. dplyr does things a little bit differently, normally you call it with the names of columns, which it does some processing on. But, when using it with a character variable, that magic fails, and you need to fall back on standard evaluation.

要修复它,您需要使用标准评估版本的 dplyr 动词(它们以 _ 结尾),并在您的函数调用中使用lazyeval。

To fix it, you need to use the standard evaluation versions of the dplyr verbs (they end in _), and use lazyeval on your function calls.

下面是使用内置mtcars数据集的代码修正:

Here's a fix for your code, using the builtin mtcars dataset:

library(lazyeval) # so we can use interpret
library(dplyr)

var1 <- "cyl"
var2 <- "mpg"
mtcars %>%
    group_by_(var1) %>% #note the _
    summarize_(Sum = interp(~sum(x), x = as.name(var2))) %>% #note the _ and the interp call
    arrange(desc(Sum))

Source: local data frame [3 x 2]

    cyl   Sum
  (dbl) (dbl)
1     4 293.3
2     8 211.4
3     6 138.2

这篇关于dplyr组由于不在Shiny中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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