Creating a Shiny Dashboard: using dateInput to populate a graph

Hello,

I'm experimenting with shiny dashboards for the first time and I'm trying to create a very basic dashboard. I created three user input features to be used within the sidebar which are all dateInputs. What I would like to do is use these three date inputs to filter my dataset and then create a bar graph which is grouped by the dates.

The issue I'm running into is the when I run my app and then input my date filters, my graph doesn't populate. I'm not sure why but I think its the way my data's date column is formatted. Additionally, is it possible to force the date calendar to not automatically pop-up whenever I go to input a date filter?



df<-data.frame(
    Cat=c("A","B","C","A","B","C","A","B","C"),
    Dates=c("092021","092021","092021","062021","062021","062021","092020","092020","092020"),
    Amount=c(500,1000,400,750,900,500,600,300,200))

df$Dates<-as.yearmon(as.Date(anydate(df$Dates)))


    ui <- dashboardPage(
        dashboardHeader(),
        dashboardSidebar( menuItem("Date Inputs",tabName = "dates",
                                   dateInput("date1", "Date1:", startview = "month",format="M yyyy", autoclose = TRUE),
                                   dateInput("date2", "Date2:", startview = "month",format="M yyyy", autoclose = TRUE),
                                   dateInput("date3", "Date3:", startview = "month",format="M yyyy", autoclose = TRUE))),
        dashboardBody(
        mainPanel(plotOutput("Invplot")))
    )
    
    server <- function(input, output) { 
        
    output$Invplot<-renderPlot({
        df%>%filter(Dates==as.yearmon(as.Date(anydate(input$date1))), Dates==as.yearmon(as.Date(anydate(input$date2))), Dates==as.yearmon(as.Date(anydate(input$date3))))%>%
            group_by(Dates)%>%ggplot(aes(Dates,Amount),fill=Cat)+geom_bar()
    })
        }
    
    shinyApp(ui, server)



Ok so there are few things going on here:

  1. If you take a look at your transformation of df$Dates, you will see the conversion is not working as intended.
  2. The filter clause in the renderPlot expression is looking for AND - based on your df, this will always result in 0 observations. Changing to OR (by using a pipe '|') should work, if this was what you were going for. I also broke out that piece for readability
  3. In your ggplot, geom_col is what you need instead of geom_bar since you are specifying a y aesthetic (e.g. Amount)
  4. In your ggplot, fill = Cat needs to be inside the aes function since it is a variable in your data

Lastly, I set an initial value for your dates for testing purposes and expanded the menuItem
Does this revision display what you were aiming for?

library(shiny)
library(shinydashboard)
library(zoo)
library(anytime)

df<-data.frame(
  Cat=c("A","B","C","A","B","C","A","B","C"),
  Dates=c("092021","092021","092021","062021","062021","062021","092020","092020","092020"),
  Amount=c(500,1000,400,750,900,500,600,300,200))

#df$Dates<-as.yearmon(as.Date(anydate(df$Dates)))
df$Dates<-as.yearmon(df$Dates, "%m%Y")


ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar( menuItem("Date Inputs",tabName = "dates",startExpanded = TRUE,
                             dateInput("date1", "Date1:", value = "2021-09-01",startview = "month",format="M yyyy", autoclose = TRUE),
                             dateInput("date2", "Date2:", value = "2020-09-01", startview = "month",format="M yyyy", autoclose = TRUE),
                             dateInput("date3", "Date3:", value = "2021-06-01",startview = "month",format="M yyyy", autoclose = TRUE))),
  dashboardBody(
    mainPanel(plotOutput("Invplot")))
)

server <- function(input, output) { 
  
  output$Invplot<-renderPlot({
    
    df_sub = df %>% 
      filter(Dates==as.yearmon(as.Date(anydate(input$date1))) | 
             Dates==as.yearmon(as.Date(anydate(input$date2))) | 
             Dates==as.yearmon(as.Date(anydate(input$date3)))) %>%
      group_by(Dates)
    
    ggplot(data = df_sub, aes(Dates,Amount,fill=Cat) ) + 
      geom_col()
  })
}

shinyApp(ui, server)
2 Likes

Really appreciate the answer and explanation.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.