0

I am learning shinydashboard in r. I have continuous data, I want to select variable and see its performance for a period of 5 years in the dashboard.

I have done the same for factors and worked well. However for continuous data I'm having challenges.

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)

The results are not filtered per each year but just one column. I expected the results to be filtered per year and plot a each value for respective year.

ismirsehregal
  • 30,045
  • 5
  • 31
  • 78
Job Kavoya
  • 91
  • 1
  • 5
  • 1
    Please provide us with a full [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – ismirsehregal Feb 05 '19 at 08:22
  • I've added full code above – Job Kavoya Feb 05 '19 at 12:10
  • Thanks! Can you please also add the used libraries and an example for your "bank" data? E.g. `dput(bank)` – ismirsehregal Feb 05 '19 at 12:19
  • library(shiny) library(shinydashboard) library(ggplot2) library(dplyr) library(ggplot2) – Job Kavoya Feb 05 '19 at 12:35
  • @ismirsehregal Data is available here https://docs.google.com/spreadsheets/d/1Q4RWAS9u8uQhf8af7XIlhTzfOVOsI0u1_QEnmkqeNKM/edit?usp=sharing – Job Kavoya Feb 06 '19 at 13:44
  • Thanks for the update. Unfortunately the headers of that file don't match with you filter statements. Do you have taken any intermediate steps after reading in the data? E.g. assigning new column names? – ismirsehregal Feb 06 '19 at 14:11

0 Answers0