【发布时间】:2015-10-18 00:58:03
【问题描述】:
每个人。我对闪亮很陌生。我想根据过滤器数据框绘制饼图。 这是我的数据框:
Duration Received Name Status
1 month 01/14/2014 Tim Completed
1 month 02/12/2014 Chris Completed
2 months 01/08/2015 Anna Completed
1 month 06/25/2014 Jenifer Completed
2 months 05/14/2015 Ruby Completed
more than 3 months 11/05/2014 David Pending
2 months 05/12/2015 Anna Completed
1 months 03/26/2015 David Completed
... ...
数据框可能超过500行
首先,我尝试按接收日期对数据框进行子集化。并在饼图中绘制 Duration Freq。例如,我想选择 2014 年 12 月 31 日之后的所有记录。所以我可以选择我的接收日期。然后,table() 子集数据框中的 Duration 列。最后,绘制饼图。
我的问题是当我更改接收日期的日期范围时,我的饼图保持一致,从不更新。谁能帮我调试我的脚本并告诉我为什么?谢谢
这是我的代码: ui.R
library(shiny)
library(ggplot2)
ggpie <- function (dat, by, totals) {
ggplot(dat, aes_string(x=factor(1), y=totals, fill=by)) +
geom_bar(stat='identity', color='black') +
guides(fill=guide_legend(override.aes=list(colour=NA))) +
coord_polar(theta='y') +
theme(axis.ticks=element_blank(),
axis.text.y=element_blank(),
axis.text.x = element_blank(),
axis.title=element_blank()) +
scale_y_continuous(breaks=cumsum(dat[[totals]]) - dat[[totals]] / 2, labels=dat[[by]])
}
shinyUI(fluidPage(
headerPanel("My App"),
sidebarPanel(
dateRangeInput("dates", label = h3("Received Date Range"),
start = "2013-01-01",
end = as.character(Sys.Date())
)
),
mainPanel(
plotOutput("Duration"),
tableOutput("test")
)
))
服务器.R
library(shiny)
library(ggplot2)
ggpie <- function (dat, by, totals) {
ggplot(dat, aes_string(x=factor(1), y=totals, fill=by)) +
geom_bar(stat='identity', color='black') +
guides(fill=guide_legend(override.aes=list(colour=NA))) +
coord_polar(theta='y') +
theme(axis.ticks=element_blank(),
axis.text.y=element_blank(),
axis.text.x = element_blank(),
#axis.text.x=element_text(colour='black',angle=45, size=10, vjust=0.5),
axis.title=element_blank()) +
scale_y_continuous(breaks=cumsum(dat[[totals]]) - dat[[totals]] / 2, labels=dat[[by]])
}
shinyServer(function(input, output) {
#table
newData= reactive({
data1 = subset(data, input$dates[1]<=data$Received && data$Received<=input$dates[2])
data2 = as.data.frame(table(data1$Duration))
})
output$test<- renderTable({
newData()
})
output$Duration<-renderPlot({
ggpie(newData(), by='Var1', totals="Freq") +
ggtitle("Duration")+
scale_fill_discrete(name="Duration")
})
})
【问题讨论】: