B3E56EC9-contents 3.82 KB
Newer Older
Sanjay Pant committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
library(tidyverse)
library(lubridate)
library(dplyr)
library(ggplot2)
library(leaflet)
library(shiny)
library(shinydashboard)

df <- read.csv(file = "crime.csv", header = TRUE, sep=",")

#remove missing values
df <- na.omit(df)

#Preprocessing
df$YEAR <- as.factor(df$YEAR)
df$HOUR <- as.factor(df$HOUR)
df$MONTH <- as.factor(df$MONTH)
df$OCCURRED_ON_DATE <- as.POSIXct(df$OCCURRED_ON_DATE, format = "%Y-%m-%d %H:%M:%OS")





row1 <- fluidRow( 
  
  tabBox(
    
    title = "Crime Frequency",
    id = "tabset1", height = "250px",
    
    tabPanel("Per Month", plotOutput("permonth")),
    tabPanel("Per Weekday", plotOutput("perday")),
    tabPanel("Per Hour", plotOutput("perhour"))
  )
  
  ,box(title = "Map View", solidHeader = TRUE 
       ,collapsible = TRUE, leafletOutput("mymap")) 
  
)

#Creating another fluid row
row2 <- fluidRow( 
  box(title = "Stacked Plot", solidHeader = TRUE 
      ,collapsible = TRUE, plotOutput("stacked"))
  
  ,box(title = "Timeseries Plot", solidHeader = TRUE 
       ,collapsible = TRUE, plotOutput("timeseries")) 
)


## Project Boston Crimes: UI - Header & Sidebars
header <- dashboardHeader(title = "Boston Crime Dataset")

sidebar <- dashboardSidebar(
  radioButtons("year", "Select Year: ",
               c("2015" = "2015",
                 "2016" = "2016",
                 "2017" = "2017",
                 "2018" = "2018",
                 "2019" = "2019")),
  
  selectInput("offense",
              "Offense Group:",
              c(unique(as.character(df$OFFENSE_CODE_GROUP[1:10])))),
  
  
  #Addition of a side bar menu to navigate to different tabs
  sidebarMenu(
    menuItem("Plots", tabName = "plots"),
    menuItem("Table", tabName = "table")
  )
  
)

#Adding components to body:


body <- dashboardBody(
  tabItems(
    tabItem("plots", row1, row2),
    tabItem("table", dataTableOutput('table'))
))


#Adding all components to UI:
ui <- dashboardPage(header, sidebar, body)



## Project Boston Crimes: Server
server <- function(input, output) { 
  
  #creating a reactive object
  filtered <- reactive({
    filter(filter(df, YEAR == input$year), OFFENSE_CODE_GROUP == input$offense)
  })
  
  #creating the plotOutput content
  output$permonth <- renderPlot({
    ggplot(data = filtered(), aes(x = filtered()$MONTH)) + geom_bar(fill="steelblue") + xlab("Month") + ylab("Number of Crimes") + theme_minimal()
  })
  
  output$perday <- renderPlot({
    ggplot(data = filtered(), aes(x = filtered()$DAY_OF_WEEK)) + geom_bar(fill="red") + xlab("Day of Week") + ylab("Number of Crimes") + theme_minimal()
  })
  
  output$perhour <- renderPlot({
    ggplot(data = filtered(),aes(x = filtered()$HOUR)) + geom_bar(fill="green") + xlab("Hour") + ylab("Number of Crimes") + theme_minimal()
  })
  
  #Defining the timeseries plot
  output$timeseries <- renderPlot({
    
    ggplot(data = filtered(), aes(x = filtered()$OCCURRED_ON_DATE))+ xlab("Date") + ylab("Number of Crimes") + geom_freqpoly(colour="purple")
  })
  
  #Defining the newly created stacked plot
  output$stacked <- renderPlot({
    
    filtered() %>% filter(DISTRICT %in% (filtered()  %>% count(DISTRICT) %>% arrange(-n) %>% head(7) %>% pull(DISTRICT)),
                          OFFENSE_DESCRIPTION %in% (filtered()  %>% count(OFFENSE_DESCRIPTION) %>% arrange(-n) %>% head(5) %>% pull(OFFENSE_DESCRIPTION))) %>% 
      ggplot(aes(DISTRICT, fill = OFFENSE_DESCRIPTION))+
      geom_bar(position = "fill")
  })
  
  output$mymap <- renderLeaflet({
    
    dm <- data.frame(latitude = filtered()$Lat, longitude = filtered()$Long)
    
    leaflet(dm) %>% addProviderTiles("Stamen.TonerLite") %>%  
      addMarkers(
        clusterOptions = markerClusterOptions()
      ) %>% setView(-71.1, 42.32, zoom = 12)
    
  })
  
  output$table <- renderDataTable(
    filtered(),
    options = list(scrollX = TRUE)
  )
  
}



## Project Boston Crimes: Building Our App
shinyApp(ui, server)