首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rowsum独立工作,但在服务器函数中不能工作

rowsum独立工作,但在服务器函数中不能工作
EN

Stack Overflow用户
提问于 2019-07-25 04:15:11
回答 1查看 58关注 0票数 1

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

我一直在rowsum和dplyr之间转来转去。

代码语言:javascript
复制
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最终产品应该显示每个团队的总数。我的结果是报告每个团队的最大值。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-25 05:08:12

通过使用tidyverse中的group_by/summarise,我们可以在dplyr中完成所有这些工作

代码语言:javascript
复制
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")
  })
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57190613

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档