Skip to content

Instantly share code, notes, and snippets.

@Emaasit
Created February 16, 2015 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Emaasit/5001f955d374fe7e9d43 to your computer and use it in GitHub Desktop.
Save Emaasit/5001f955d374fe7e9d43 to your computer and use it in GitHub Desktop.
This script is used to produce heatmaps
# this R script is used to develop crash density plots using kernel density method
# the purpose is to identify locations with high crashes
# install the required packages
install.packages("rjson")
install.packages("devtools")
library(devtools)
install_github("rstudio/leaflet")
install_github("ramnathv/rCharts")
install_github("ramnathv/rMaps")
install.packages("dplyr")
install.packages("brew")
library(shiny)
library(leaflet)
library(RColorBrewer)
library(scales)
library(lattice)
library(dplyr)
library(brew)
# start by creating a map of the location
L2<-Leaflet$new()
# set the view to Tennessee
L2$setView(c(35.7882005,-86.3301614), 7)
# create tile Layer
L2$tileLayer(provider="MapQuestOpen.OSM")
# read the data into R
ped_crash_locations<-readRDS("data/densityPlot/ped_locations.rds")
bike_crash_locations<-readRDS("data/densityPlot/bike_locations.rds")
# create a JSONArray file for bike and ped locations
ped_data<-ped_crash_locations[c("lat","lon")]
bike_data<-bike_crash_locations[c("lat","lon")]
ped_data_Json<-toJSONArray2(ped_data, json = F, names = F)
cat(rjson::toJSON(ped_data_Json))
bike_data_Json<-toJSONArray2(bike_data, json = F, names = F)
cat(rjson::toJSON(bike_data_Json))
# now add heatmaps for bike and ped crashes
# Add leaflet-heat plugin. Thanks to Vladimir Agafonkin
L2$addAssets(jshead = c(
"http://leaflet.github.io/Leaflet.heat/dist/leaflet-heat.js"
))
# Add javascript to modify underlying chart
L2$setTemplate(afterScript = sprintf("
<script>
var addressPoints = %s
var heat = L.heatLayer(addressPoints).addTo(map)
</script>
", rjson::toJSON(ped_data_Json)
))
print(L2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment