Chris
Chris

Reputation: 307

Shiny dashboard - reactive bar plot with values from checkbox

Based on my data, I can prepare a simple dodge bar plot

geo <- data.frame("year" = c(2018, 2018, 2018, 2019, 2019, 2019, 2020, 2020, 2020), 
                   "geo" = c("Europe", "Asia", "America", "Europe", "Asia", "America", "Europe", "Asia", "America"), 
                   "sales" = c(100, 150, 200, 500, 500, 500, 1200, 1800, 1200)) 

ggplot(geo, aes(fill=as.factor(year), x=geo, y=sales))+
   geom_bar(position="dodge", stat = "identity")

I want to put this plot in the Shiny dashboard, together with a checkbox to select years. I do

library(shiny)
ui <- dashboardPage(
  dashboardHeader(title = "Basic dashboard"),
  dashboardSidebar(),
  dashboardBody(
    # Boxes need to be put in a row (or column)
    fluidRow(
     
      box(plotOutput("plot1", height = 250)),
      
      box(
       
        checkboxGroupInput("checkGroup", label = "Year",                          
                           choices = list("2018"=2018, "2019" = 2019, "2020" = 2020),
                           selected = 2018)
      )
    )
  )
)

server <- function(input, output) {

  output$plot1 <- renderPlot({
    ggplot(geo, aes(fill=as.factor(input$checkGroup), x=geo, y=sales))+
      geom_bar(position="dodge", stat = "identity")
    
  })
}

shinyApp(ui, server)

I get two types of errors:

  1. The values on the plot don't change according to the checkbox selection - the plot is the same for every year (and the values are wrong)
  2. I cannot generate plot with more than one year selected. I get the followinng error: "Error in : Aesthetics must be either length 1 or the same as the data (9): fill"

Can anyone help?

Upvotes: 2

Views: 592

Answers (1)

stefan
stefan

Reputation: 123978

This could be achieved like so:

  1. You have to filter the dataset according to the checked years. To this end use a reactive.
  2. In the renderPlot use the data frame returned by the reactive for plotting and simply map year on fill


    library(shiny)
    library(shinydashboard)
    
    geo <- data.frame("year" = c(2018, 2018, 2018, 2019, 2019, 2019, 2020, 2020, 2020), 
                      "geo" = c("Europe", "Asia", "America", "Europe", "Asia", "America", "Europe", "Asia", "America"), 
                      "sales" = c(100, 150, 200, 500, 500, 500, 1200, 1800, 1200)) 
    
    ui <- dashboardPage(
      dashboardHeader(title = "Basic dashboard"),
      dashboardSidebar(),
      dashboardBody(
        # Boxes need to be put in a row (or column)
        fluidRow(
          
          box(plotOutput("plot1", height = 250)),
          
          box(
            
            checkboxGroupInput("checkGroup", label = "Year",                          
                               choices = list("2018"=2018, "2019" = 2019, "2020" = 2020),
                               selected = 2018)
          )
        )
      )
    )
    
    server <- function(input, output) {
      
      dat <- reactive({
        filter(geo, year %in% input$checkGroup)
      })
      
      output$plot1 <- renderPlot({
        ggplot(dat(), aes(fill=as.factor(year), x=geo, y=sales))+
          geom_bar(position="dodge", stat = "identity")
        
      })
    }
    
    shinyApp(ui, server)

enter image description here

Upvotes: 2

Related Questions