A reactive graph that takes in four inputs that adds the totals together based on what is selected

I am using the suicide data set from Kaggle here: https://www.kaggle.com/russellyates88/suicide-rates-overview-1985-to-2016. I am trying to create a shiny app that takes in country, year, age and sex and will output the suicide_no column based on what has been selected from the drop downs. For example if I selected Albania from 1987 it will add up all totals no matter age range and sex but if I then add males it will remove the females.
UI:

ui <- dashboardPage(
  dashboardHeader(title = "Global Suicide Rates"),
  
  dashboardSidebar(
    sidebarMenu(
      
      selectInput("nation", "Select a Country: ", choices = sort(unique(suicideRate$ï..country)), selected = NULL,
                  multiple = TRUE, selectize = TRUE, width = NULL, size = NULL),
      selectInput("year", "Select a Year: ", choices = sort(unique(suicideRate$year)), selected = NULL,
                  multiple = TRUE, selectize = TRUE, width = NULL, size = NULL),
      selectInput("sex", "Select a Sex: ", choices = sort(unique(suicideRate$sex)), selected = NULL,
                  multiple = TRUE, selectize = TRUE, width = NULL, size = NULL),
      selectInput("age", "Select an Age Range: ", choices = sort(unique(suicideRate$age)), selected = NULL,
                  multiple = TRUE, selectize = TRUE, width = NULL, size = NULL),
      actionButton("goButton", "Apply")
      
    )
  ),
 
  dashboardBody(
    
    fluidRow(
      box(width = 6, solidHeader = TRUE, status = "primary",
          title = "Total", 
          plotlyOutput("suicide", width = '750px', height = '300px')
          
      )
      )
  )
)

Server:

server <- function(input, output, session) {
  
  filteredData <- reactive({
    req(input$nation)
    filtSuicideRate <- suicideRate
    
    if(!is.null(input$nation)) {
      filtSuicideRate <- filtSuicideRate %>% filter(ï..country %in% input$nation)
    }
    if(!is.null(input$year)) {
      filtSuicideRate <- filtSuicideRate %>% filter(year %in% input$year)
    }
    if(!is.null(input$sex)) {
      filtSuicideRate <- filtSuicideRate %>% filter(sex %in% input$sex)
    }
    if(!is.null(input$age)) {
      filtSuicideRate <- filtSuicideRate %>% filter(age %in% input$age)
    }
    
    filtSuicideRate
  })
  
  output$suicide <- renderPlotly({
    input$goButton
    plot_ly(filteredData(), x = ~ï..country, y = count(suicideRate$suicides_no), type = 'bar')
    })
  
}

Here is a modified version where I used eventReactive() to produce filteredData so that you can choose all of the filter settings before clicking the Apply button to produce the graph. I also changed the data processing going into the plot_ly to use summarize() so that you get the sum of suicides_no, not the number of rows of data. The x axis of the plot is not correct yet but I don't know enough plot_ly to fix that at the moment.

library(shiny)
library(ggplot2)
library(shinydashboard)
library(plotly)
suicideRate <- read.csv("master.csv")
ui <- dashboardPage(
  dashboardHeader(title = "Global Suicide Rates"),
  
  dashboardSidebar(
    sidebarMenu(
      
      selectInput("nation", "Select a Country: ", choices = sort(unique(suicideRate$ï..country)), selected = NULL,
                  multiple = TRUE, selectize = TRUE, width = NULL, size = NULL),
      selectInput("year", "Select a Year: ", choices = sort(unique(suicideRate$year)), selected = NULL,
                  multiple = TRUE, selectize = TRUE, width = NULL, size = NULL),
      selectInput("sex", "Select a Sex: ", choices = sort(unique(suicideRate$sex)), selected = NULL,
                  multiple = TRUE, selectize = TRUE, width = NULL, size = NULL),
      selectInput("age", "Select an Age Range: ", choices = sort(unique(suicideRate$age)), selected = NULL,
                  multiple = TRUE, selectize = TRUE, width = NULL, size = NULL),
      actionButton("goButton", "Apply")
      
    )
  ),
  
  dashboardBody(
    
    fluidRow(
      box(width = 6, solidHeader = TRUE, status = "primary",
          title = "Total", 
          plotlyOutput("suicide", width = '750px', height = '300px')
          
      )
    )
  )
)

server <- function(input, output, session) {
  
  filteredData <- eventReactive(input$goButton,{
    req(input$nation)
    filtSuicideRate <- suicideRate
    
    if(!is.null(input$nation)) {
      filtSuicideRate <- filtSuicideRate %>% filter(ï..country %in% input$nation)
    }
    if(!is.null(input$year)) {
      filtSuicideRate <- filtSuicideRate %>% filter(year %in% input$year)
    }
    if(!is.null(input$sex)) {
      filtSuicideRate <- filtSuicideRate %>% filter(sex %in% input$sex)
    }
    if(!is.null(input$age)) {
      filtSuicideRate <- filtSuicideRate %>% filter(age %in% input$age)
    }
    
    filtSuicideRate
  })
  
  output$suicide <- renderPlotly({
    filteredData() %>% group_by(ï..country) %>%  summarize(SumSuicide = sum(suicides_no)) %>% #count(suicideRate$suicides_no)
    plot_ly(x = ~ï..country, y = ~SumSuicide , type = 'bar')
  })
  
  
  
}

shinyApp(ui = ui, server = server)

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