简体   繁体   English

R Shiny - 如何使用函数 dateRangeInput 和 selectInput 创建反应式条形图

[英]R Shiny - How to create a reactive barplot with functions dateRangeInput and selectInput

my question is probably simple but I don't find an answer.I generate a dataframe (Surveillance) with two diseases (Diseases_1 and Disease_2).I would like to make a barplot that reacts according to dates and the choice of disease.我的问题可能很简单,但我没有找到答案。我生成了一个包含两种疾病(Diseases_1 和 Disease_2)的数据框(Surveillance)。我想制作一个根据日期和疾病选择做出反应的条形图。 PS : I agregate data by week. PS:我按周汇总数据。 My UI is here :我的用户界面在这里:

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

Surveillance <- data.frame(
  Date = seq(as.Date("2015/1/1"), as.Date("2015/12/31"), "days"),
  Disease_1 = as.numeric(sample(1:100,365,replace=T)), Disease_2 =as.numeric(sample(1:100,365,replace=T)))
Surveillance <- Surveillance %>% mutate(
  Week = format(Date, "%Y-%m-%U"))

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

My server is here :我的服务器在这里:

server <- function(input, output) {

  dateRangeInput<-reactive({
    dataset <- subset(Surveillance, Date >= input$daterange[1] & Date <= input$daterange[2])
    dataset = switch(input$Diseases,
                     "Disease_1" = Disease_1,
                     "Disease_2" = Disease_2)  
    dataset
  })

 output$barplot <-renderPlot({
    ggplot(data=dateRangeInput(), aes_string(x="Week",y=input$Diseases))  + 
      stat_summary(fun.y = sum, geom = "bar",colour="#56B4E9",fill="#56B4E9") +
      geom_bar(stat="identity") + 
      labs(title=input$Diseases, y ="Number") +
      theme_classic() + 
      theme(plot.title = element_text(hjust = 0.5))
  })

}
shinyApp (ui = ui, server = server)

When I run the program, R Shiny diplays : ERROR object 'Disease_1' not found.当我运行程序时,R Shiny 显示:找不到错误对象“Disease_1”。 I change many times the program but I don't find my mistake(s).我多次更改程序,但我没有发现我的错误。 Coud you help me please ?你能帮我吗? I thank you.我谢谢你。

refer to ggplot docs,参考ggplot文档,

  1. data in ggplot should only be dataframe data在ggplot应该只是dataframe
  2. aes_string parameters refer to columns of data aes_string参数指的是数据列

Code代码

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

Surveillance <- data.frame(
  Date = seq(as.Date("2015/1/1"), as.Date("2015/12/31"), "days"),
  Disease_1 = sample(1:100,365,replace=T), Disease_2 =sample(1:100,365,replace=T))
Surveillance <- Surveillance %>% mutate(
  Week = format(Date, "%Y-%m-%U"))

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


server <- function(input, output) {

  dateRangeInput<-reactive({
    dataset <- subset(Surveillance, Date >= input$daterange[1] & Date <= input$daterange[2]) 
    dataset <- dataset[,c(input$Diseases, "Week")]
    dataset
  })


  output$barplot <-renderPlot({
    ggplot(data=dateRangeInput(), aes_string(x="Week",y=input$Diseases))  +
      stat_summary(fun.y = sum, geom = "bar",colour="#56B4E9",fill="#56B4E9") +
      geom_bar(stat="identity") +
      labs(title=input$Diseases, y ="Number") +
      theme_classic() +
      theme(plot.title = element_text(hjust = 0.5))
  })

}
shinyApp (ui = ui, server = server)

情节

You did not have any variable called Disease_1 , that was just a column in surveillance dataframe.您没有任何名为Disease_1变量,它只是surveillance数据Disease_1一列。 You need to pass a dataframe when user selects Disease_1 as their choice, which is the default selection for your case.当用户选择Disease_1作为他们的选择时,您需要传递一个数据Disease_1 ,这是您案例的默认选择。 You have been passing something called Disease_1 which does not exists and therefore, it fails.你一直在传递一个叫做Disease_1东西,它不存在,因此它失败了。

Here is a working code for you:这是您的工作代码:

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

    Surveillance <- data.frame(
      Date = seq(as.Date("2015/1/1"), as.Date("2015/12/31"), "days"),
      Disease_1 = as.numeric(sample(1:100,365,replace=T)), Disease_2                 =as.numeric(sample(1:100,365,replace=T)))

    Surveillance <- Surveillance %>% mutate(
      Week = format(Date, "%Y-%m-%U"))

    ui <- fluidPage(
      dateRangeInput("daterange", "Choice the date",
                     start = min(Surveillance$Date),
                     end = max(Surveillance$Date),
                     min = min(Surveillance$Date),
                     max = max(Surveillance$Date),
                     separator = " - ", format = "dd/mm/yy",        
                     startview = 'Week', language = 'fr', weekstart = 1),  

      selectInput(inputId = 'Diseases',
                  label='Diseases',
                  choices=c('Disease_1','Disease_2'),
                  selected='Disease_1'),

      plotOutput("barplot"))


    server <- function(input, output) {

      dateRangeInput<-reactive({
        dataset <- subset(Surveillance, Date >= input$daterange[1] & Date <=         input$daterange[2])
        dataset = switch(input$Diseases,
                         "Disease_1" =         Surveillance[,setdiff(colnames(Surveillance),'Disease_2')],
                         "Disease_2" =         Surveillance[,setdiff(colnames(Surveillance),'Disease_1')])  
        print(head(dataset))
        return(dataset)
      })

      output$barplot <-renderPlot({

        if(is.null(dateRangeInput())) return()

        ggplot(data=dateRangeInput(), aes_string(x="Week",y=input$Diseases))          + 
          stat_summary(fun.y = sum, geom =         "bar",colour="#56B4E9",fill="#56B4E9") +
          geom_bar(stat="identity") + 
          labs(title=input$Diseases, y ="Number") +
          theme_classic() + 
          theme(plot.title = element_text(hjust = 0.5))        
      })        

    }
    shinyApp (ui = ui, server = server)

Delete删除

dataset = switch(input$Diseases,
                     "Disease_1" = Disease_1,
                     "Disease_2" = Disease_2) 

and it will work它会起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM