大家。我很有光泽。我想基于过滤数据框绘制饼图。 这是我的数据框:
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行
首先,我尝试按接收日期对数据框进行子集化。并在饼图中绘制持续时间频率。例如,我想在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")
)
))
server.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")
})
})
答案 0 :(得分:4)
只需更改
input$dates[1]<=data$Received && data$Received<=input$dates[2]
到
input$dates[1]<=data$Received & data$Received<=input$dates[2]
&&
与&
的含义不同。对于前者,您不会在整个向量data$Received
上进行比较。见help("&&")