Home > OS >  R Shiny - Dynamically filter ggplot2 chart using DateSlider
R Shiny - Dynamically filter ggplot2 chart using DateSlider

Time:11-12

There are lot of related questions like this, I tried them didnt workout so I am posting a new question.

My Sample data

Week_End    Product nts
2021-10-22  A   17
2021-10-15  B   12
2021-10-08  C   18
2021-10-01  A   37
2021-09-24  B   46
2021-09-17  C   27
2021-09-10  A   31
2021-09-03  A   45
2021-08-27  B   23
2021-08-20  B   12

I plotted a Barchart using the code

server <- function(input, output,session) {
    nt_data <- reactive({
        chart_nts <- perf_ind %>%
            filter(product %in% input$productid & (week_end >= input$start_dt & week_end <= input$end_dt)) %>%
            group_by(week_end,product) %>%
            summarise(c_nts = sum(nts))
    })
    
    observe({
        updateSelectizeInput(session,"productid",choices = prod_dim$prod_nm)
    })    
    
    
    output$ntsplot <- renderPlot({
        dateid<-input$dateid
        
        g <- ggplot(nt_data(),aes(y= c_nts, x = week_end))
        g   geom_bar(stat = "sum")
    })
    
} 

my UI code looks like

sidebarLayout(position = "left",
                  sidebarPanel(
                      selectizeInput("productid", "Select product","Names"),
                      sliderInput("dateid",
                                  "Slide your Date:",
                                  min = as.Date(date_range$start_dt,"%Y-%m-%d"),
                                  max = as.Date(date_range$end_dt,"%Y-%m-%d"),
                                  value=as.Date(date_range$asofdate,"%Y-%m-%d"),
                                  timeFormat="%Y-%m-%d")
                      ),
                  
                  mainPanel(
                      fluidRow(
                          splitLayout(cellWidths = c("50%", "50%"), plotOutput("ntsplot"), plotOutput(""))
                      )
                  )
                  )

All I need is when I use Date Slider my chart should change accordingly, for that I have done this

output$ntplot <- renderPlot({
        dateid<-input$dateid
        data <- nt_data %>%
        filter (week_end >= input$start_dt & week_end <= input$end_dt) %>%  
        g <- ggplot(data(),aes(y= c_nts, x = week_end))
        g   geom_bar(stat = "sum")
    })

and

nt_data <- reactive({
        chart_nts <- perf_ind %>%
            filter(product %in% input$productid & (week_end >= input$start_dt & week_end <= input$end_dt)) %>%
            group_by(week_end,product) %>%
            summarise(c_nts = sum(nts))
    })

DateRange values I am fetching it from the database.

When I execute I am getting following error

Warning: Error in : Problem with `filter()` input `..1`.
x Input `..1` must be of size 4842 or 1, not size 0.

What I am missing here help me understanding!! Thanks for your help!!

CodePudding user response:

Not sure of how you want to use sliderInput. I replaced it with dateRangeInput(). Try this

prod <- read.table(text=
"week_end    product nts
2021-10-22  A   17
2021-10-15  B   12
2021-10-08  C   18
2021-10-01  A   37
2021-09-24  B   46
2021-09-17  C   27
2021-09-10  A   31
2021-09-03  A   45
2021-08-27  B   23
2021-08-20  B   12", header=T)

ui <- fluidPage(
  sidebarLayout(position = "left",
                sidebarPanel(
                  selectizeInput("productid", "Select product","Names"),
                  dateRangeInput("date_range", "Period you want to see:",
                                 start = min(prod$week_end),
                                 end   = max(prod$week_end),
                                 min   = min(prod$week_end),
                                 max   = max(prod$week_end)
                  )#,
                  # sliderInput("dateid",
                  #             "Slide your Date:",
                  #             min = as.Date(date_range$start_dt,"%Y-%m-%d"),
                  #             max = as.Date(date_range$end_dt,"%Y-%m-%d"),
                  #             value=as.Date(date_range$asofdate,"%Y-%m-%d"),
                  #             timeFormat="%Y-%m-%d")
                ),
                
                mainPanel(
                  fluidRow(
                    splitLayout(cellWidths = c("50%", "50%"), plotOutput("ntplot"), DTOutput("t1"))
                  )
                )
  )
)

server <- function(input, output,session) {
  nt_data <- reactive({
    chart_nts <- prod %>%
      dplyr::filter(product %in% input$productid & (week_end >= input$date_range[1] & week_end <= input$date_range[2])) %>%
      group_by(week_end,product) %>%
      dplyr::summarise(c_nts = sum(nts))
    data.frame(chart_nts)
  })
  
  output$t1 <- renderDT({nt_data()})
  
  observe({
    updateSelectizeInput(session,"productid",choices = prod$product)
  })    
  
  output$ntplot <- renderPlot({
    #dateid<-input$dateid
    data <- nt_data() # %>% dplyr::filter(week_end >= input$date_range[1] & week_end <= input$date_range[2]) 
    g <- ggplot(data,aes(y= c_nts, x = week_end))  
           geom_bar(stat = "identity")
    g
  })

  
} 

shinyApp(ui, server)

output

  • Related