Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffquinn-nuna committed Feb 13, 2017
1 parent d479848 commit 0b2719a
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 0 deletions.
13 changes: 13 additions & 0 deletions R/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Choropleth Shiny Server
=======================

`run_shiny_server.sh` will take a CSV file as an argument, and it will start a shiny app that
plots a choropleth of the SF neighborhoods using that data.

For example:

```
./run_shiny_server.sh ../data/San_Francisco_Development_Pipeline_2015_Quarter_4.csv
```

The shiny app will allow you to select which variable the choropleth color scale corresponds to.
3 changes: 3 additions & 0 deletions R/run.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
library(shiny)

runApp()
3 changes: 3 additions & 0 deletions R/run_shiny_server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
# Usage: ./run_shiny_server.sh <path to csv>
Rscript run.R "$1"
32 changes: 32 additions & 0 deletions R/server.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
library(plyr)
library(rgeos)
library(maptools)
library(stringr)
library(ggplot2)
library(shiny)

args <- commandArgs(trailingOnly = TRUE)

df = read.csv(args[1], stringsAsFactors = FALSE)

neighborhoods <- readShapeSpatial("../gis/shp/neighborhoods.shp")
neighborhoods <- fortify(neighborhoods, region = "nhood")

# Try to standardize identifiers.
neighborhoods$id <- str_trim(toupper(neighborhoods$id))
df$NEIGHBORHOOD <- str_trim(toupper(df$NEIGHBORHOOD))

plotChoropleth = function(data, variable) {
ggplot() +
geom_map(data = data,
aes_string(map_id = "NEIGHBORHOOD", fill = variable),
map = neighborhoods) +
geom_polygon(data = neighborhoods, aes(x=long, y = lat, group = group), fill = NA, color = "black") +
expand_limits(x = neighborhoods$long, y = neighborhoods$lat)
}

shinyServer(function(input, output) {
output$choropleth <- renderPlot({
plotChoropleth(df, input$variable)
})
})
22 changes: 22 additions & 0 deletions R/ui.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require(shiny)
require(plyr)

args <- commandArgs(trailingOnly = TRUE)
df = read.csv(args[1], stringsAsFactors=FALSE)

# Only integer columns will work well for the choropleth
options = names(colwise(class)(df)[,colwise(class)(df) == "integer"])
variableSelector = selectInput(inputId = "variable",
label = "Variable",
choices = options,
selected = "UNITS")

shinyUI(fluidPage(
titlePanel("SF Housing Pipeline"),
sidebarLayout(
mainPanel(
plotOutput("choropleth", height = "700px")
),
sidebarPanel(variableSelector)
)
))
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions gis/shp/convert_geojson_shp.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

ogr2ogr -nlt POLYGON -skipfailures "$2".shp "$1" OGRGeoJSON
Binary file added gis/shp/neighborhoods.dbf
Binary file not shown.
1 change: 1 addition & 0 deletions gis/shp/neighborhoods.prj
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]
Binary file added gis/shp/neighborhoods.shp
Binary file not shown.
Binary file added gis/shp/neighborhoods.shx
Binary file not shown.

0 comments on commit 0b2719a

Please sign in to comment.