如何使连续变量具有反应性?

时间:2019-02-03 20:02:55

标签: r shiny shinydashboard

我正在r中学习Shinydashboard。我有连续的数据,我想选择变量,并在仪表板上查看其在5年内的性能。

我在各种因素上都做过同样的事情,并且运作良好。但是对于连续数据,我遇到了挑战。

library(shiny)
library(shinydashboard)
library(ggplot2)
library(dplyr)
library(ggplot2) 

bank <- data.frame(
           Year = c(2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
                    2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017),
           GNPL = c(5.050917, 5.038851, 5.06826, 5.04099, 5.048787, 4.804405,
                    4.973502, 4.754165, 4.79148, 4.783482, 4.760701, 4.723932,
                    4.79181, 4.913056, 5.034628, 5.168294, 5.331172, 5.422618),
        Pb4_Tax = c(3.446848, 3.947287, 3.762228, 4.140822, 4.180126, 4.529122,
                    4.432264, 4.55145, 4.626268, 4.68954, 4.870825, 4.951595,
                    5.033013, 5.099543, 5.149665, 5.12716, 5.16863, 5.124491),
       T_Assets = c(5.638028, 5.628351, 5.659622, 5.71248, 5.761664, 5.804107,
                    5.878123, 5.978286, 6.073225, 6.131458, 6.224821, 6.305527,
                    6.367418, 6.431909, 6.505068, 6.543154, 6.567725, 6.602357),
        T_Loans = c(5.435988, 5.389124, 5.41216, 5.442061, 5.53527, 5.496416,
                    5.56549, 5.66028, 5.782359, 5.834383, 5.927831, 6.046995,
                    6.096741, 6.169584, 6.287977, 6.335524, 6.35658, 6.334158),
        Capital = c(4.757904, 4.729384, 4.726491, 4.744183, 4.818107, 4.900989,
                    4.942767, 5.085012, 5.201618, 5.266359, 5.377051, 5.448893,
                    5.547678, 5.621336, 5.71696, 5.740215, 5.783046, 5.795351),
   Core_capital = c(4.67532, 4.652072, 4.645648, 4.734119, 4.789101, 4.876789,
                    4.92718, 5.055462, 5.152218, 5.216691, 5.330775, 5.394893,
                    5.48858, 5.558042, 5.638852, 5.667727, 5.716643, 5.739242),
            ROA = c(0.5, 1.6, 1, 2.2, 2.1, 3.7, 2.4, 2.6, 2.6, 2.6, 3.6, 4.4,
                    4.6, 4.7, 4.4, 2.9, 3.2, 2.6),
    Total_staff = c(4.107956, 4.073865, 4.054613, 4.054268, 4.077041, 4.099991,
                    4.192233, 4.335598, 4.406387, 4.417173, 4.460086, 4.477931,
                    4.500182, 4.532232, 4.567297, 4.558853, 4.527565, 4.493542)
)


header <- dashboardHeader(title = "Kenya Banks KPIs 2000 to 2017")
sidebar <- dashboardSidebar(
  sidebarMenu(
    #menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
    selectInput("year", h3("Select Year"),
                choices = list(2000,2001,2002,2003,2004,2005,2006,
                               2007,2008,2009,2010,2011,2012,2013,
                               2014, 2015, 2016, 2017), selected = 2015),
    checkboxGroupInput("financial", h3("Select Financials"), 
                       choices = c("NPL", "Profit", "Assets", "Loans", "Capital", "Core Capital", "ROA", "Year"),
                       selected = "ROA"),
    checkboxGroupInput("staff", h3("Staff Category"), 
                       choices = c("Management", "Supervisory", "Clerical & Support", "Support staff", "Total Staff"),
                       selected = "Total Staff")

  )
)

frow1 <- fluidRow(
  infoBoxOutput("Capital"),
  infoBoxOutput("Profit"),
  infoBoxOutput("NPLs")
)

frow2 <- fluidRow(
  box(
    title = "Financial Performance",
    status = "danger",
    solidHeader = TRUE,
    collapsible = TRUE,
    plotOutput("plot1", height = "600")
  ),
  box(
    title = "Staff composition",
    status = "success",
    solidHeader = TRUE,
    collapsible = TRUE,
    plotOutput("plot2", height = "600")
  )
)

body <- dashboardBody(frow1, frow2)
ui <- dashboardPage(title = 'Kenya Commercial Banks Performance Indicators', header, sidebar, body, skin = 'blue')

server <- function(input, output) {

  output$Capital <- renderInfoBox({
    infoBox(
      strong("Capital 2017 (M USD)"), bank[18, 6]/100, icon = icon("usd"),
      color = 'lime', fill = T
    )
  })

  output$Profit <- renderInfoBox({
    infoBox(
      strong("Profit 2017 (M USD)"), bank[18, 3]/100, icon = icon("wallet", lib = "font-awesome"),
      color = "blue", fill = T
    )
  })

  output$NPLs <- renderInfoBox({
    infoBox(
      strong("NPLs 2017(USD)"), bank[18, 2]/100, icon = icon("balance-scale", lib = "font-awesome"),
      color = "orange", fill = T
    )
  })

  output$plot1 <- renderPlot({
    filtered <- bank %>% filter(Capital %in% input$financial)
    ggplot(filtered, aes(Capital)) + geom_point()
  })

  output$plot2 <- renderPlot({
    filtered_1 <- bank %>% filter(Management %in% input$staff)
    ggplot(filtered_1, aes(Management)) + geom_point()
  })
}

shinyApp(ui = ui, server = server)

不是每年过滤结果,而仅过滤一列。我希望每年对结果进行过滤,并绘制相应年份的每个值。

0 个答案:

没有答案