Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit cef492a384d8ad5739114c12261c62ce26acba15 @blmoore committed May 16, 2015
Showing with 198 additions and 0 deletions.
  1. +13 −0 blackspot.Rproj
  2. BIN data/accidents.rds
  3. +8 −0 rsconnect/shinyapps.io/blmr/blackspot.dcf
  4. +67 −0 server.R
  5. +57 −0 styles.css
  6. +53 −0 ui.R
@@ -0,0 +1,13 @@
+Version: 1.0
+
+RestoreWorkspace: Default
+SaveWorkspace: Default
+AlwaysSaveHistory: Default
+
+EnableCodeIndexing: Yes
+UseSpacesForTab: Yes
+NumSpacesForTab: 2
+Encoding: UTF-8
+
+RnwWeave: Sweave
+LaTeX: pdfLaTeX
Binary file not shown.
@@ -0,0 +1,8 @@
+name: blackspot
+account: blmr
+server: shinyapps.io
+bundleId: 187383
+url: https://blmr.shinyapps.io/blackspot
+when: 1431813071.37007
+asMultiple: FALSE
+asStatic: FALSE
@@ -0,0 +1,67 @@
+library("dplyr")
+library("DT")
+library("ggplot2")
+library("htmltools")
+library("leaflet")
+library("RColorBrewer")
+library("shiny")
+library("stringr")
+library("zoo")
+
+accidents <- readRDS("data/accidents.rds")
+accidents <- accidents[sample(1:nrow(accidents), 1000),]
+
+accident_desc <- function(row)
+ with(as.list(row), paste0(strong(
+ format.Date(a_date, "%a %d %B %Y"), ": "), "A ",
+ tolower(severity), " collision in a ", speed_limi,
+ " MPH zone, involving ", no_vehicle, " vechicle(s) with ",
+ no_casualt, " casualtie(s). Weather was ", tolower(weather)))
+
+strs <- apply(accidents, 1, accident_desc)
+strs <- str_wrap(strs, width=10)
+
+# summary plot munging
+accidents$ym <- as.factor(zoo::as.yearmon(accidents$a_date))
+d2 <- accidents %>% group_by(as.factor(ym)) %>%
+ summarise(n=n())
+colnames(d2) <- c("ym", "n")
+
+# colour paletters
+pal <- brewer.pal(9, "Set1")
+
+# clean table for dt
+clean <- accidents[,c(3:5,6:7, 17, 22, 23, 24, 25, 35)]
+rownames(clean) <- NULL
+colnames(clean) <- c("Severity", "No. vehicles",
+ "No. casualties", "Date", "Day", "Speed limit",
+ "Light conditions", "Weather conditions",
+ "Road conditions", "Special conditions", "Postcode")
+
+shinyServer(function(input, output) {
+
+ output$mymap <- renderLeaflet({
+ fillv <- if(input$color == "None") "black" else pal[as.factor(accidents$severity)]
+
+ leaflet(data=accidents) %>%
+ addTiles(urlTemplate="http://openmapsurfer.uni-hd.de/tiles/roadsg/x={x}&y={y}&z={z}") %>%
+ addTiles('http://{s}.tile.openstreetmap.se/hydda/roads_and_labels/{z}/{x}/{y}.png',
+ attribution='&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>') %>%
+ setView(lng=-3.19, lat=55.95, zoom=13) %>%
+ addCircleMarkers(~long, ~lat, radius=~(no_vehicle+.8)**1.5, fillOpacity=input$alpha,
+ color=NA, popup=strs, weight=2, fillColor = fillv)
+ })
+
+ output$monthTotals <- renderPlot({
+ ggplot(d2, aes(x=zoo::as.Date(zoo::as.yearmon(ym)), y=n)) +
+ geom_area() + theme_minimal() +
+ labs(x="", y="Recorded collisions") +
+ scale_y_continuous(expand=c(0,0))
+ })
+
+ output$table <- DT::renderDataTable({
+ DT::datatable(clean, filter = 'top', options = list(
+ pageLength = 10, autoWidth = TRUE))
+ })
+
+})
@@ -0,0 +1,57 @@
+input[type="number"] {
+ max-width: 80%;
+}
+
+div.outer {
+ position: fixed;
+ top: 41px;
+ left: 0;
+ right: 0;
+ bottom: 0;
+ overflow: hidden;
+ padding: 0;
+}
+
+/* Customize fonts */
+body, label, input, button, select {
+ font-family: 'Helvetica Neue', Helvetica;
+ font-weight: 200;
+}
+h1, h2, h3, h4 { font-weight: 400; }
+
+#controls {
+ /* Appearance */
+ background-color: white;
+ padding: 0 20px 20px 20px;
+ cursor: move;
+ /* Fade out while not hovering */
+ opacity: 0.65;
+ zoom: 0.9;
+ transition: opacity 500ms 1s;
+}
+#controls:hover {
+ /* Fade in while hovering */
+ opacity: 0.95;
+ transition-delay: 0;
+}
+
+/* Position and style citation */
+#cite {
+ position: absolute;
+ bottom: 10px;
+ left: 10px;
+ font-size: 12px;
+}
+
+/* If not using map tiles, show a white background */
+.leaflet-container {
+ background-color: white !important;
+}
+
+.navbar-brand {
+ font-weight: bold;
+}
+
+div.leaflet-control-zoom {
+ margin-top: 65px !important;
+}
@@ -0,0 +1,53 @@
+library("dplyr")
+library("DT")
+library("shiny")
+library("leaflet")
+
+
+shinyUI(navbarPage("Blackspot", id="nav",
+ tabPanel("Map",
+ div(class="outer",
+
+ tags$head(
+ includeCSS("styles.css")
+ ),
+
+ leafletOutput("mymap", width="100%", height="100%"),
+
+ # Shiny versions prior to 0.11 should use class="modal" instead.
+ absolutePanel(id = "controls", class = "panel panel-default", fixed = TRUE,
+ draggable = TRUE, top = 120, left = "auto", right = 20, bottom = "auto",
+ width = 400, height = "auto",
+
+ h2("Edinburgh traffic collisions"),
+ hr(),
+
+ h3("Controls"),
+
+ #selectInput("color", "Color", vars),
+ #selectInput("size", "Size", vars, selected = "adultpop"),
+
+ dateRangeInput('dates',
+ label = 'Occurred between:',
+ start = as.Date("2010-01-01"), end = as.Date("2013-07-01")),
+
+ selectInput("color", "Color by:",
+ choices=c("None", "Severity", "Casualties", "Time", "Vehicles")),
+
+ sliderInput("alpha", "Opacity:",
+ min=0, max=1, value=.3),
+
+ h3("Summary plots"),
+ plotOutput("monthTotals", height = 150)
+ #plotOutput("scatterCollegeIncome", height = 250)
+ ),
+
+ tags$div(id="cite",
+ 'Traffic collision data from Edinburgh County Council via',
+ tags$a("Edinburgh Open Data", href="http://www.edinburghopendata.info/dataset/vehicle-collisions"),
+ ". Shiny code adapted from",
+ tags$a("Superzip", href="https://github.com/jcheng5/superzip"), "by Joe Cheng.")
+ )
+ ), tabPanel("Table", DT::dataTableOutput("table"))
+)
+)

0 comments on commit cef492a

Please sign in to comment.