4

I'm having trouble getting group_by in dplyr work while using Shiny. It seems like dplyr doesn't recognize the input$var from Shiny as being a valid field in the table.

In this example, I want the "level" input in ui.R to dictate what to group by.

In ui.R I have:

library(shiny)
shinyUI(fluidPage(
titlePanel("Orders"),
sidebarLayout(
sidebarPanel(
  selectInput("Region_Input", label = h5("Choose a Region"), 
              choices = list("A", "B")),
  radioButtons("level", "What level do you want to see:",
                     list("item", "category"))

),
mainPanel(
  verbatimTextOutput("Level_Select"),
  tableOutput(outputId="table")

))))

In server.R I have:

library(shiny)  
library(dplyr)

OrderItems <- data.frame(Region =      c('A','A','A','A','A','A','B','B','B','B','B','B','B'),
                     item = c('Item A','Item B','Item C','Item D','Item E',
                              'Item A','Item B','Item C','Item D','Item E',
                              'Item A','Item B','Item C'),
                     category = c('Cat 1','Cat 1','Cat 1','Cat 2','Cat 2',
                                  'Cat 1','Cat 1','Cat 1','Cat 2','Cat 2',
                                  'Cat 1','Cat 1','Cat 1'))
shinyServer(
function(input, output) {

output$table <- renderTable({
  OrderItems %>%
    group_by(input$level) %>%
    summarize(count = n()) %>%
    arrange(desc(count))    
})
})

The output I expect when the "level" input is "category" is:

   category count
1   Cat 1   9
2   Cat 2   4

However, what I end up getting is:

    input$level count
1   category           13

Any ideas on how to fix this would be greatly appreciated!

SFuj
  • 925
  • 1
  • 9
  • 14

2 Answers2

6

Per @joran's comment, this should be as simple as replacing group_by(input$level) with group_by_(input$level), but its tough to say without a reproducible example. If you make a few changes and leverage the mtcars data, one can reproduce the following to see how this works:

server.R

library(shiny)  
library(dplyr)

shinyServer(
  function(input, output) {

    output$table <- renderTable({
      mtcars %>%
        group_by_(input$level) %>%
        summarize(count = n()) %>%
        arrange(desc(count))    
    })
  })

ui.R

library(shiny)
shinyUI(fluidPage(
  titlePanel("Orders"),
  sidebarLayout(
    sidebarPanel(
      selectInput("Region_Input", label = h5("Choose a Region"), 
                  choices = list("A", "B")),
      radioButtons("level", "What level do you want to see:",
                   list("cyl", "am"))

    ),
    mainPanel(
      verbatimTextOutput("Level_Select"),
      tableOutput(outputId="table")

    ))))
JasonAizkalns
  • 20,243
  • 8
  • 57
  • 116
  • 2
    Beware if there may be multiple grouping vars coming from `input$level`. See https://stackoverflow.com/questions/27688193/ and as commented, you could use `group_by_(.dots = input$level)` in that case – talat Aug 24 '15 at 16:53
1

For latest dplyr version, you can use:

group_by(across(input$level))
Wang
  • 1,314
  • 14
  • 21