rowsum 独立工作,但在服务器功能内部时不工作

rowsum works independently, but not when inside a server function

当在函数外部使用 rowsum 按团队汇总值时它可以工作,但当放入仪表板的服务器函数内时,它返回最大值。

我一直在rowsum和dplyr之间徘徊。

install.packages(c("shiny","shinydashboard","ggplot2","dplyr"))
#this is just a subset of the entire data frame
data.frame(Team =  
  c("Blue","Blue","Blue","Green","Green","Green","Gold","Gold","Gold") 
  ,Revenue = c(1455,1462,3440,900,2299,1139,2472,2830,1789))

header <- dashboardHeader(title = "July 2019")
sidebar <- dashboardSidebar(
  sidebarMenu(
    menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard"))
  )
)

frow2 <- fluidRow(
  box(
    title = "Revenue by Team"
    ,status = "primary"
    ,solidHeader = TRUE
    ,collapsible = TRUE
    ,plotOutput("team.revenue", height = "400px")
  )
)

body <- dashboardBody(frow2)
ui <- dashboardPage(title = "Title", header, sidebar, body)

server <- function(input, output) {
  team.revenue <-  rowsum(July$Revenue, July$Team, reorder = TRUE)
output$team.revenue <- renderPlot({
    ggplot(data = July,
           aes(x=Team, y=Revenue , fill = factor(July$Team)))+
      geom_bar(position = "dodge", stat = "identity") + ylab("Revenue")+
      xlab("Team") + theme(legend.position = "bottom"
                           ,plot.title = element_text(size = 15, face = 
"bold"))+
      ggtitle("Revenue by Team") + labs(fill = "Team")
  })
}
shinyApp(ui, server)

没有错误messages.The 最终产品应显示每个团队的总数。我的结果报告了每个团队的最大值。

我们可以通过使用 dplyr

中的 group_by/summarisetidyverse 中完成所有这些
library(dplyr)
library(ggplot2)
library(shiny)
library(shinydashboard)
server <- function(input, output) {

  output$team.revenue <- renderPlot({
    July %>%
         group_by(Team) %>%
         summarise(Revenue = sum(Revenue)) %>%
    ggplot(data =.,
           aes(x=Team, y=Revenue , fill = factor(Team)))+
      geom_bar(position = "dodge", stat = "sum") + ylab("Revenue")+
      xlab("Team") + theme(legend.position = "bottom"
                           ,plot.title = element_text(size = 15, face = 
                                                        "bold"))+
      ggtitle("Revenue by Team") + labs(fill = "Team")
  })
}