Base R Aggregate Function in Shiny (varying user input in ,by argument of aggregate func)

Hi Friends,

Based on grouping variable selection by user want data to be aggregated. There are 5 variables any combination of that can be used for grouping. Below is simplified version with 2 grouping variables.
The below code works but need to work for varying inputs (Grouping Variable) as per user

Can you please suggest if this can be done with the base R aggregate or there is some other way to do it.

library(shiny)

m1<-rnorm(20,100,sd=20)
m2<-rnorm(20,120,sd=40)
m3<-rnorm(20,140,sd=60)
g1<-rep(c("a","b"),10)
g2<-rep(c("c","d","e","f"),5)

data<-data.frame(m1,m2,m3,g1,g2)

ui<-fluidPage(
 
 selectInput("Grp","Grp",choices=c("g1","g2"),multiple=TRUE),
 textOutput("text1"),
 tableOutput("agg_table")
 
)

server<-function(input,output,session)
{
 output$text1<-renderText({input$Grp[1]})
 
   grp_data <- reactive({
      
   aggregate(data[,1:3],by=list(data[[input$Grp[1]]],data[[input$Grp[2]]]),sum)
   
 })
 
   output$agg_table <-renderTable( grp_data())
 
}

shinyApp(ui=ui,server=server)

```r

I recommend you learn dplyr/tidyverse
the group_by(), summarise() syntax is easy to learn and use

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.