2 min read

Shiny Apps for Analyzing Dallas Police Data

Exploring Dallas Murders

Exploring Dallas Murders

Forecasting Dallas Crime Rates

Forecasting Dallas Crime

Exploring Dallas Trafficking

Exploring Dallas Trafficking

To explore the Dallas Crime data more yourself, check out my Dallas Crime Explorer Shiny app here.

To explore the Dallas Murder data more yourself, check out my Dallas Murder Shiny app here.

Dallas Trafficking

To explore the Dallas Trafficking data yourself, check out my Dallas Trafficking Shiny app here.

To explore any recorded incidents of human trafficking in the Dallas area, I used the following code:

setDT(PI)
traffic <- PI[grepl("TRAFFICKING",offincident),]
traffic[,NumIncidentsPerYear := .N,by = "servyr"]

traffic[,rowid := 1:.N]
traffic[,Date := as.Date(substr(date1,1,10))]
traffic[,LatLongStart := regexpr("(",geocoded_column,fixed = TRUE)[1] + 1,by = rowid]
traffic[,LatLongEnd := regexpr(")",geocoded_column,fixed = TRUE)[1] - 1,by = rowid]
traffic[,LatLong := substr(geocoded_column,start = LatLongStart,stop = LatLongEnd)]
traffic[,LatLongComma := regexpr(",",LatLong,fixed = TRUE)[1],by = rowid]
traffic[,Latitude := substr(LatLong,start = 1,stop = LatLongComma - 1)]
traffic[,Longitude := substr(LatLong,start = LatLongComma + 1,stop = 1000)]
traffic[,Longitude := as.numeric(Longitude)]
traffic[,Latitude := as.numeric(Latitude)]

# Manually fix two missing geocodes
traffic[is.na(Latitude) & incident_address == "14040 N STEMMONS SERV",Latitude := 32.93769989950343]
traffic[is.na(Longitude) & incident_address == "14040 N STEMMONS SERV",Longitude := -96.90205446873641]

traffic[is.na(Latitude) & incident_address == "7815 L B J FWY",Latitude := 32.925475281010286]
traffic[is.na(Longitude) & incident_address == "7815 L B J FWY",Longitude := -96.77161085979215]

traffic[,Year := as.factor(servyr)]

Here’s the code to create the Leaflet map within the Shiny app:

        leaflet(data = traffic) %>% 
            addProviderTiles(providers$Stamen.TonerLite,options = providerTileOptions(noWrap = TRUE)) %>% 
                addCircleMarkers(lng = ~Longitude, lat = ~Latitude, weight = 2, radius = input$MarkerSize,
                                 fillOpacity = input$MarkerOpacity,fillColor = ~pal(Year),color = "gray",stroke = 1,
                                 popup = ~paste0("DPD Incident Date: ",traffic$Date,", Incident Address: ",incident_address,
                                                 ", Victim Sex: ",traffic$compsex,
                                                ", Victim Age: ",traffic$compage,", MO: ",traffic$mo)) %>%
                addLegend(position = "bottomleft",
                          pal = pal, values = ~traffic$Year,
                          title = "Legend",
                          opacity = 1)

The full code behind the Shiny App is on GitHub here.