Group_by dplyr function in brilliant

I am having trouble getting group_by in dplyr when using Shiny. It seems dplyr does not recognize the input variable $ var from Shiny as a valid field in the table.

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

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 that I expect when "level" is entered is "category":

  category count 1 Cat 1 9 2 Cat 2 4 

However, I get:

  input$level count 1 category 13 

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

0
source share
1 answer

In @joran's comment, this should be as simple as replacing group_by(input$level) with group_by_(input$level) , but it's hard to say without a reproducible example. If you make a few changes and use the mtcars data, you can reproduce the following to see how it 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") )))) 
+2
source

Source: https://habr.com/ru/post/973569/


All Articles