library("tidyverse")
library("leaflet")
library("zoo")
library("ggthemes")
library("knitr")
library("shiny")

# Import data sets
incident_2020 <- read_csv("Police_Incidents_2020.csv")
incident_2021 <- read_csv("Police_Incidents_2021.csv")

# Adjust data type and concatenate 
incident_2021$precinct <- as.character(incident_2021$precinct)
incident_raw <- bind_rows(incident_2020, incident_2021)


# Select relevant columns only
incident_selected <- incident_raw %>% select(publicaddress, reportedDateTime,description, centerLong,centerLat, neighborhood,offense)

# Parse datetime on reporteDateTime column

incident_selected$reportedDateTime <- substr(incident_selected$reportedDateTime, 1,16)
incident_selected$reportedDateTime <- as.character(incident_selected$reportedDateTime)
incident_selected$reportedDateTime <- parse_datetime(incident_selected$reportedDateTime, "%Y/%m/%d %H:%M")
incident_selected <- incident_selected %>% mutate(reportedDate = format(reportedDateTime, "%Y/%m")) 

# Clean NA and zero value row 
incident_selected <- incident_selected[incident_selected$centerLong != 0,]

# Plot on map by leaflet
incident_selected %>% leaflet() %>% 
  addProviderTiles(providers$Stamen.TonerLite) %>% 
  addMarkers(label = incident_selected$description,
             clusterOptions = markerClusterOptions(),
             lng = incident_selected$centerLong, lat = incident_selected$centerLat,
             popup = paste("<b>","Neighborhood:","</b>",
                           incident_selected$neighborhood,"<br>",
                           "<b>","Address:","</b>",
                           incident_selected$publicaddress,"<br>",
                           "<b>","Time:","</b>",
                           incident_selected$reportedDateTime)) %>%
  addMiniMap(toggleDisplay = TRUE,tiles = providers$Stamen.TonerLite)