group_by dplyr功能闪亮 [英] group_by dplyr function in shiny
问题描述
在使用闪光时,我遇到了在dplyr工作中遇到group_by问题。看来,dplyr不能将来自Shiny的$ var的输入识别为表中的有效字段。
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.
在这个例子中,我想要 ui.R来决定什么是分组。
In this example, I want the "level" input in ui.R to dictate what to group by.
在ui.RI中有:
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")
))))
在server.RI中:
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!
推荐答案
Per @ joran的评论,这应该是替换 group_by(input $ level)
with group_by_(input $ level)
,但很难说没有可重现的例子。如果您进行了一些修改并利用 mtcars
数据,可以重现以下内容,以了解其运作方式:
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:
library(shiny)
library(dplyr)
shinyServer(
function(input, output) {
output$table <- renderTable({
mtcars %>%
group_by_(input$level) %>%
summarize(count = n()) %>%
arrange(desc(count))
})
})
ui.R
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")
))))
这篇关于group_by dplyr功能闪亮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!