dplyr group by not working in Shiny

一曲冷凌霜 提交于 2019-12-18 17:02:09

问题


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

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

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


回答1:


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.

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.

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


来源:https://stackoverflow.com/questions/34481864/dplyr-group-by-not-working-in-shiny

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!