rshinydynamic-columnsfiltered

How to get the sum of the filtered data in shinyapp


I'm trying my first shinyapp using two filters. The output would be two; the first to get a data table and the second to get the sum of the filtered output.

The data consists of 4 variables; Welfare_benefit - character Value - Numerical Category - character Targeted_group - character

Data - Budget2023

Code:

library(shiny)
library(dplyr)
library(readxl)
library(shinythemes)

Budget2023 <- read_excel("Budget2023.xlsx", 
                         sheet = "Welfare benefits for R")

Budget2023$`Value_in_Rupees_Million` <- as.numeric(Budget2023$`Value_in_Rupees_Million`)

ui <- fluidPage(

 theme = shinytheme("superhero"),
    titlePanel("Budget 2023 - Welfare allocations"),

    sidebarLayout(
        sidebarPanel(
            selectInput("category",
                        "Select category",
                        choices =   Budget2023$`Broad_categories identified`),
        selectInput("group",
                    "Select the targeted group",
                    choices = Budget2023$`Targetted_group`)
            ),

        # Show a plot of the generated distribution
        mainPanel(
           tableOutput("WelfareTable"),
           textOutput("Total")
        )
    )
)




server <- function(input, output) {

  
output$WelfareTable <- renderTable( { 
  if (input$category == "") {
    subset(Budget2023, input$group == Budget2023$`Targetted_group`)
  } else {
    subset(Budget2023, input$category == Budget2023$`Broad_categories_identified` & input$group == Budget2023$`Targetted_group`)
  }
  })  


output$Total <- renderText(c(sum(Budget2023$`Value_in_Rupees_Million`))) 


# Run the application 
shinyApp(ui = ui, server = server)

The issue is it doesn't generate the sum of the columns filtered and generated in the Welfaretable output.

Appreciate if you can assist on this as I couldn't figure out the issue through existing examples.

TIA!

Edit:

Dataset - Budget2023

Budget2023 <- DF ( Welfare_benefit = c('A','B', 'C', 'D', 'E', 'F', 'G'),
                   Value_in_Rupees_Million = c(43000, 67000, 3000, 250, 5700,340, 3100), 
                   Broad_categories_identified = c('Economic', 'Education', 'Health', 'Education', 'Livelihood', 'Education'),
                   Targetted_group = c('Elderly', 'Disabled', 'Children', 'Low income', 'Elderly', 'Disabled', 'Disabled'))

Solution

  • the text output in your code is not using the subset table, but taking sum of all your Budget2023 dataset. try this, I'm using a reactive that generates the subset using input selections and use it in the output table and text calculating sum.

    Budget2023 <- data.frame( Welfare_benefit = c('A','B', 'C', 'D', 'E', 'F', 'G'),
         Value_in_Rupees_Million = c(43000, 67000, 3000, 250, 5700,340, 3100), 
         Broad_categories_identified = c('Economic', 'Education', 'Health', 'Education', 'Livelihood', 'Education', 'Education'),
         Targetted_group = c('Elderly', 'Disabled', 'Children', 'Low income', 'Elderly', 'Disabled', 'Disabled'))
    
    ui <- fluidPage(
      theme = shinytheme("superhero"),
      titlePanel("Budget 2023 - Welfare allocations"),
      
      sidebarLayout(
        sidebarPanel(
          selectInput("category", selected = "", # initial value
                      "Select category",
                      choices =   Budget2023$Broad_categories_identified),
          selectInput("group",
                      selected = Budget2023$Targetted_group[1], # initial value
                      "Select the targeted group",
                      choices = Budget2023$Targetted_group)
        ),
        # Show a plot of the generated distribution
        mainPanel(
          tableOutput("WelfareTable"),
          textOutput("Total")
        )
      )
    )
    
    server <- function(input, output) {
      subset_data <- reactive({
        if (input$category == "") {
          subset(Budget2023, input$group == Budget2023$Targetted_group)
        } else {
          subset(Budget2023, input$category == Budget2023$Broad_categories_identified & input$group == Budget2023$Targetted_group)
        }
      })
      
      output$WelfareTable <- renderTable( { 
        subset_data()
      })  
      
      output$Total <- renderText(sum(subset_data()$Value_in_Rupees_Million))
    }
      
      # Run the application 
      shinyApp(ui = ui, server = server)