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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With