Shiny dashboard - reactive bar plot with values from checkbox

Chris

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?

stefan

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Reactive bar plot with different datasets in Shiny

Shiny dashboard reactive table with links

Shiny ggvis reactive plot

R Shiny: passing reactive values to and from modules

How to download data of bar chart from shiny dashboard?

Preventing Shiny selectInput from evaluating prematurely when updating a reactive plot

Render a plot generated from a fitted model in reactive for shiny

How is data passed from reactive Shiny expression to ggvis plot?

Save reactive plot to temp directory as png from shiny app

questions in shiny with selectinput and the reactive plot

Dynamic filters and reactive plot in Shiny

Code for a reactive table to plot in Shiny

Returning a dataframe in a reactive function in R Shiny Dashboard

Reactive uiOutput in shiny flexdashboard -- kills entire dashboard?

Reactive bar chart in shiny R

Reactive Bar Chart in Shiny with gather

Shiny reactive values based on different reactive values

How do I control bar colors in plot_ly with reactive data in shiny

shiny: how to add a reactive bar in shiny app

How to insert reactive input values from a shiny app into a MySQL database?

How to send reactive values from Shiny App into global environment for review?

How to save reactive values in Shiny?

If Statement in Shiny based on values in reactive

dynamically plot multiple variables in shiny dashboard R

How do you create a stacked percentage bar chart in R Shiny app with two reactive values?

Adjust the width of the header bar in dashboardHeader in shiny dashboard

Disable the vertical scroll bar in shiny dashboard

R shiny error when using reactive to plot

R Shiny: How can I return reactive values from a shiny module to the master server function?