R Shiny - 如何创建根据时间单位(周,月,年)作出反应并按时间单位聚合数据的条形图

时间:2017-06-21 10:23:52

标签: r date shiny dplyr reactive

我使用以下变量生成有关疾病的数据框:

  • 日期(疾病日期)
  • 案例(案件数量,默认情况下,案件数量为1)
  • 周(疾病周病例)
  • 月(病例月)
  • 年(疾病年)。

我的ui在这里:

library(shiny) 
library(dplyr)
library(lubridate)
library(ggplot2)
library(scales)

Disease<-data.frame(Date=seq(as.Date("2015/1/1"), as.Date("2017/1/1"), "days"),Cases=rep(1))
Disease$Week<-as.Date(cut(Disease$Date,breaks="week",start.on.monday = TRUE))
Disease$Month<-as.Date(cut(Disease$Date,breaks="month"))
Disease$Year<-as.Date(cut(Disease$Date,breaks="year"))

ui <- fluidPage(
      dateRangeInput("daterange", "Choice the date",
      start = min(Disease$Date),
      end = max(Disease$Date),
      min = min(Disease$Date),
      max = max(Disease$Date),
      separator = " - ", format = "dd/mm/yy",
      startview = 'Month', language = 'fr', weekstart = 1),
      selectInput(inputId = 'Time_unit',
      label='Time_unit',
      choices=c('Week','Month','Year'),
      selected='Month'),
plotOutput("Disease"))

我希望创建一个根据时间单位(即周,月,年)作出反应的条形图,并按时间单位格式化数据

您将在下面找到服务器的代码(但它不起作用):

server <- function(input, output) {
       dateRangeInput<-reactive({
       dataset= subset(Disease, Date >= input$daterange[1] & Date <= 
       input$daterange[2])
       return(dataset)
       })
selectInput= reactive({
summarize(group_by(dateRangeInput(),
period = switch(input$Time_unit,
"Week"=Disease$Week,
"Month" = Disease$Month,
"Year" = Disease$Year)))
})

output$Disease <-renderPlot({
ggplot(data=selectInput(), aes_string(x="period",x="Cases"))  
+ stat_summary(fun.y = sum, geom = "bar") 
+ labs(title="Disease", y ="Number of cases")
+theme_classic() 
+theme(plot.title = element_text(hjust = 0.5))
})

}
shinyApp (ui = ui, server = server)

如果我选择周,月或年,你会发现我想要获得的以下条形图:

抱歉&#34;按年份&#34;,我无法发布2个以上的链接

1 个答案:

答案 0 :(得分:0)

您可以检查此代码,它的工作方式如下:

library(shiny) 
library(dplyr)
library(lubridate)
library(ggplot2)
library(scales)

Disease<-data.frame(Date=seq(as.Date("2015/1/1"), as.Date("2017/1/1"), "days"),Cases=rep(1))
Disease <- Disease %>% mutate(Week = format(Date, "%Y-%m-%U"),Month = format(Date, "%Y-%m"), Year = format(Date, "%Y"))

ui <- fluidPage(
  dateRangeInput("daterange", "Choice the date",
                 start = min(Disease$Date),
                 end = max(Disease$Date),
                 min = min(Disease$Date),
                 max = max(Disease$Date),
                 separator = " - ", format = "dd/mm/yy",
                 startview = 'Month', language = 'fr', weekstart = 1),
  selectInput(inputId = 'Time_unit',
              label='Time_unit',
              choices=c('Week','Month','Year'),
              selected='Month'),
  plotOutput("Disease"))


server <- function(input, output) {
  dateRangeInput<-reactive({
    dataset <- subset(Disease, Date >= input$daterange[1] & Date <= input$daterange[2])
    dataset
  })
  selectInput= reactive({
    dataset <- dateRangeInput() %>% group_by_(input$Time_unit) %>% summarise(Sum = sum(Cases))
    print(head(dataset))
    dataset
    })

  output$Disease <-renderPlot({
    ggplot(data=selectInput(), aes_string(x=input$Time_unit,y="Sum"))  + geom_bar(stat="identity") + 
    labs(title="Disease", y ="Number of cases") +
    theme_classic() + 
    theme(plot.title = element_text(hjust = 0.5))
  })

}
shinyApp (ui = ui, server = server)

Plot对Time_unit输入有反应,你只需要对轴文本进行小调整即可。

P.S。您到图像的第二个链接

  <周>

无效 - &gt;得到一个空的空间。