6. Final_Dashboard.R 3.47 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
library(tidyverse)
library(lubridate)
library(dplyr)
library(ggplot2)
library(leaflet)
library(shiny)
library(shinydashboard)

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

Sanjay Pant committed
11 12 13
#remove missing values
df <- na.omit(df)

14 15 16 17 18 19 20 21 22 23 24
#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( 
Sanjay Pant committed
25
  
26
  tabBox(
Sanjay Pant committed
27
    
28 29
    title = "Crime Frequency",
    id = "tabset1", height = "250px",
Sanjay Pant committed
30
    
31 32 33 34
    tabPanel("Per Month", plotOutput("permonth")),
    tabPanel("Per Weekday", plotOutput("perday")),
    tabPanel("Per Hour", plotOutput("perhour"))
  )
Sanjay Pant committed
35
  
36 37
  ,box(title = "Map View", solidHeader = TRUE 
       ,collapsible = TRUE, leafletOutput("mymap")) 
Sanjay Pant committed
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
)

#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")) 
)


#Adding components to body:
body <- dashboardBody(row1, row2)



## 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",
Sanjay Pant committed
68 69
              "Offense Group:",
              c(unique(as.character(df$OFFENSE_CODE_GROUP[1:10]))))
70 71 72 73 74 75 76 77 78 79
  
)

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



## Project Boston Crimes: Server
server <- function(input, output) { 
Sanjay Pant committed
80 81 82 83 84
  
  #creating a reactive object
  filtered <- reactive({
    filter(filter(df, YEAR == input$year), OFFENSE_CODE_GROUP == input$offense)
  })
85 86 87

  #creating the plotOutput content
  output$permonth <- renderPlot({
Sanjay Pant committed
88
    ggplot(data = filtered(), aes(x = filtered()$MONTH)) + geom_bar(fill="steelblue") + xlab("Month") + ylab("Number of Crimes") + theme_minimal()
89
  })
Sanjay Pant committed
90
  
91
  output$perday <- renderPlot({
Sanjay Pant committed
92
    ggplot(data = filtered(), aes(x = filtered()$DAY_OF_WEEK)) + geom_bar(fill="red") + xlab("Day of Week") + ylab("Number of Crimes") + theme_minimal()
93
  })
Sanjay Pant committed
94
  
95
  output$perhour <- renderPlot({
Sanjay Pant committed
96
    ggplot(data = filtered(),aes(x = filtered()$HOUR)) + geom_bar(fill="green") + xlab("Hour") + ylab("Number of Crimes") + theme_minimal()
97
  })
Sanjay Pant committed
98
  
99 100 101
  #Defining the timeseries plot
  output$timeseries <- renderPlot({
    
Sanjay Pant committed
102
   ggplot(data = filtered(), aes(x = filtered()$OCCURRED_ON_DATE))+ xlab("Date") + ylab("Number of Crimes") + geom_freqpoly(colour="purple")
103 104 105 106
  })

  #Defining the newly created stacked plot
  output$stacked <- renderPlot({
Sanjay Pant committed
107 108 109 110 111
    
    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")
112 113 114 115
  })

  output$mymap <- renderLeaflet({

Sanjay Pant committed
116
        dm <- data.frame(latitude = filtered()$Lat, longitude = filtered()$Long)
117 118 119 120 121 122 123 124 125 126 127 128 129 130
    
    leaflet(dm) %>% addProviderTiles("Stamen.TonerLite") %>%  
      addMarkers(
        clusterOptions = markerClusterOptions()
      ) %>% setView(-71.1, 42.32, zoom = 12)
    
  })
  
}



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