0

I am trying to build a shiny app that gives user the flexibility to choose the variables for group by and summarize. Checkbox will have an option for selecting group by variables. Right now I haven't given measure variables as selections, since I struggling with group by. I want the numbers to be aggregated basis the selection.

library(shiny)
library(ggplot2)  # for the diamonds dataset
library(shinydashboard)
library(dplyr)
y1<-diamonds
ui <- fluidPage(
  checkboxGroupInput("variable", "Variables to show:",
                     c("cut","color","clarity"),selected = "cut"),
  tableOutput("data"),
  textOutput("result")
)

server <- function(input, output, session) {
  base <- reactive({ 
    groupby <- enquo(input$variable)
    print(groupby)

    res <-y1%>% group_by(!!!groupby,x) %>% 
      tally() %>% 
      ungroup() %>% 
      summarise(sum = sum(x)) %>% 
      pull()
    res 
  }) 
  output$result <- renderText({
    input$variable
  })




  output$data<-renderTable({
    base()  

  }



  )
}

shinyApp(ui, server)

Thanks, Hema

user35655
  • 13
  • 5

2 Answers2

0

I'm not sure that I understood your question exactly, but maybe something like this:

library(shiny)
library(ggplot2)  # for the diamonds dataset
library(shinydashboard)
library(dplyr)
y1<-diamonds
ui <- fluidPage(
  checkboxGroupInput("variable", "Variables to show:",
                     c("cut","color","clarity"),selected = "cut"),
  tableOutput("data"),
  textOutput("result")
)

server <- function(input, output, session) {
  base <- reactive({ 
    res <- y1 %>% group_by(eval(parse(text = input$variable)),x) %>% 
      tally() %>% 
      #ungroup() %>% 
      summarise(sum = sum(x)) %>% 
      pull()
    res 
  }) 

  output$result <- renderText({
    input$variable
  })

  output$data<-renderTable({
    base()  
  })
}

shinyApp(ui, server)
motch
  • 458
  • 4
  • 13
  • Thank you motch. It partially solves the problem. I am trying to get the header and the levels of the selected categorical variables. – user35655 Jan 27 '20 at 03:59
  • This worked for me. (https://stackoverflow.com/questions/50092344/multiple-group-by-in-shiny-app) Thank you motch, your solution gave me a lead. – user35655 Jan 27 '20 at 04:30
0

if it's possible to select multiple from the check boxes such that you'd want something like group_by(x,y) this may help you get what you want:

 group_by(across(all_of(input$group)))