R shiny reactive table,渲染图无法工作

R shiny reactive table, render plot can not work

各位。我对闪亮很陌生。我想绘制一个基于过滤器数据框的饼图。 这是我的数据框:

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 多行

首先,我尝试按接收日期对数据框进行子集化。并在饼图中绘制持续时间频率。例如,我想选择 12/31/2014 之后的所有记录。所以我可以 select 我的接收日期。然后,子集数据框中的 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")    
  })
})

只是改变

input$dates[1]<=data$Received && data$Received<=input$dates[2]

input$dates[1]<=data$Received & data$Received<=input$dates[2]

&&& 的含义不同。对于前者,您并不是在对整个向量 data$Received 进行比较。参见 help("&&")