【发布时间】:2019-05-20 05:25:26
【问题描述】:
为了做一个仪表板,我正在学习一些闪亮的东西。我有个主意。我想创建一个仪表板,从 selectinput 中选择一个变量,按此类变量分组并绘制该变量总数的条形图或直方图。
我已经生成了一个示例数据集来生成我需要的东西,但是我无法得到我需要的东西。
UI代码是下一个:
library(shiny)
shinyUI(fluidPage(
titlePanel("Demo dashboard"),
sidebarLayout(
sidebarPanel(
selectInput("variable",
"group by",
choices = c("City","Country")
)
),
mainPanel(
plotOutput("distPlot")
)
)
))
服务器代码是下一个,这里我按输入变量聚合并绘制总数
library(shiny)
library(dplyr)
shinyServer(function(input, output) {
output$distPlot <- renderPlot({
sample<-tbl_df(data.frame(c("City1","City2","City3","City1","City2","City3","City2","City3"),
c("A","B","C","D","D","A","A","B"),
c(12,14,15,12,12,14,8,10)))
colnames(sample)<-c("City","Country","Amount")
df1<-sample%>%group_by(input$variable)%>%
summarise(total=sum(Amount))
sample%>%group_by(input$variable)%>%summarise(total=sum(Amount))
x<- df1$total
hist(x)
})
})
我的结果的屏幕截图是下一个:
但这不是预期的结果。我无法获得所需的直方图
【问题讨论】:
-
嗨!你的问题现在有点模糊:“我无法得到我需要的东西。”不是很多信息。例如,您可以截屏并注释问题吗?
标签: shiny shinydashboard