R语言 如何在shinyapp中获得过滤数据的总和

mwkjh3gx  于 2022-12-20  发布在  其他
关注(0)|答案(1)|浏览(109)

我正在试用我的第一个使用两个过滤器的shinyapp。第一个用于获得数据表,第二个用于获得滤波输出的和。
数据由4个变量组成;福利_效益-字符值-数值类别-字符目标_群体-字符
数据-2023年预算
代码:

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)

问题是它不会生成Welfaretable输出中过滤和生成的列的总和。
如果你能在这方面提供帮助,我将不胜感激,因为我无法通过现有的例子来解决这个问题。
蒂亚!
编辑:

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'))
1sbrub3j

1sbrub3j1#

这文本输出在你的代码是不再使用这子集表格,但是采取总和所有你的Budget2023 dataset.尝试这,我正在使用一个reactive那产生子集使用input选择并且使用它在这输出表格和文本计算总和.

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)

相关问题