Skip to content

Instantly share code, notes, and snippets.

@ramnathv
Last active December 20, 2015 01:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ramnathv/6050562 to your computer and use it in GitHub Desktop.
Save ramnathv/6050562 to your computer and use it in GitHub Desktop.
Bike Share Shiny App
require(RJSONIO); require(rCharts); require(RColorBrewer); require(httr)
options(stringsAsFactors = F)
getNetworks <- function(){
require(httr)
if (!file.exists('networks.json')){
url <- 'http://api.citybik.es/networks.json'
dat <- content(GET(url))
writeLines(dat, 'networks.json')
}
networks <- RJSONIO::fromJSON('networks.json')
nms <- lapply(networks, '[[', 'name')
names(networks) <- nms
return(networks)
}
getData <- function(network = 'citibikenyc'){
require(httr)
url = sprintf('http://api.citybik.es/%s.json', network)
bike = fromJSON(iconv(content(GET(url)), to = "UTF-8"))
bike2 <- lapply(bike, function(station){within(station, {
fillColor = cut(
as.numeric(bikes)/(as.numeric(bikes) + as.numeric(free)),
breaks = c(0, 0.20, 0.40, 0.60, 0.80, 1),
labels = brewer.pal(5, 'RdYlGn'),
include.lowest = TRUE
)
popup = iconv(whisker::whisker.render(
'<b>{{name}}</b><br>
<b>Free Docks: </b> {{free}} <br>
<b>Available Bikes:</b> {{bikes}}
<p>Retreived At: {{timestamp}}</p>'
), from = 'latin1', to = 'UTF-8')
latitude = as.numeric(lat)/10^6
longitude = as.numeric(lng)/10^6
lat <- lng <- NULL})
})
return(bike2)
}
getCenter <- function(nm, networks){
net_ = networks[[nm]]
lat = as.numeric(net_$lat)/10^6;
lng = as.numeric(net_$lng)/10^6;
return(list(lat = lat, lng = lng))
}
networks <- getNetworks()
plotData <- function(data_, network = 'citibikenyc'){
center_ <- getCenter(network, networks)
L1 <- Leaflet$new()
L1$tileLayer(provider = 'Stamen.TonerLite')
L1$set(height = 800, width = 1600)
L1$setView(c(center_$lat, center_$lng), 13)
L1$geoJson(toGeoJSON(data_),
onEachFeature = '#! function(feature, layer){
layer.bindPopup(feature.properties.popup)
} !#',
pointToLayer = "#! function(feature, latlng){
return L.circleMarker(latlng, {
radius: 4,
fillColor: feature.properties.fillColor || 'red',
color: '#000',
weight: 1,
fillOpacity: 0.8
})
} !#"
)
L1$enablePopover(TRUE)
L1$fullScreen(TRUE)
return(L1)
}
[{"city": "", "name": "valenbisi", "url": "http://api.citybik.es/valenbisi.json", "radius": 20000, "lat": 39456480, "lng": -355509, "id": 0}, {"city": "", "name": "sevici", "url": "http://api.citybik.es/sevici.json", "radius": 20000, "lat": 37371724, "lng": -6003312, "id": 1}, {"city": "", "name": "dublin", "url": "http://api.citybik.es/dublin.json", "radius": 20000, "lat": 53330662, "lng": -6260177, "id": 2}, {"city": "", "name": "cyclocity", "url": "http://api.citybik.es/cyclocity.json", "radius": 20000, "lat": 36700744, "lng": 137212211, "id": 3}, {"city": "", "name": "bizi", "url": "http://api.citybik.es/bizi.json", "radius": 20000, "lat": 41650822, "lng": -869839, "id": 4}, {"city": "", "name": "velib", "url": "http://api.citybik.es/velib.json", "radius": 20000, "lat": 48874575, "lng": 2356796, "id": 5}, {"city": "", "name": "velo", "url": "http://api.citybik.es/velo.json", "radius": 20000, "lat": 43608020, "lng": 1428450, "id": 6}, {"city": "", "name": "veloh", "url": "http://api.citybik.es/veloh.json", "radius": 20000, "lat": 49637130, "lng": 6164580, "id": 7}, {"city": "", "name": "villo", "url": "http://api.citybik.es/villo.json", "radius": 20000, "lat": 50837726, "lng": 4381565, "id": 8}, {"city": "", "name": "bicing", "url": "http://api.citybik.es/bicing.json", "radius": 20000, "lat": 41398298, "lng": 2153128, "id": 9}, {"city": "", "name": "cristolib", "url": "http://api.citybik.es/cristolib.json", "radius": 20000, "lat": 49026274, "lng": 2016950, "id": 10}, {"city": "", "name": "cyclic", "url": "http://api.citybik.es/cyclic.json", "radius": 20000, "lat": 49431521, "lng": 1102925, "id": 11}, {"city": "", "name": "velostanlib", "url": "http://api.citybik.es/velostanlib.json", "radius": 20000, "lat": 48687831, "lng": 6187801, "id": 12}, {"city": "", "name": "bicloo", "url": "http://api.citybik.es/bicloo.json", "radius": 20000, "lat": 47224514, "lng": -1553711, "id": 13}, {"city": "", "name": "velocite", "url": "http://api.citybik.es/velocite.json", "radius": 20000, "lat": 47239621, "lng": 6017275, "id": 14}, {"city": "", "name": "barclays", "url": "http://api.citybik.es/barclays.json", "radius": 20000, "lat": 51500743, "lng": -202759, "id": 15}, {"city": "", "name": "ecobici", "url": "http://api.citybik.es/ecobici.json", "radius": 20000, "lat": 19434353, "lng": -99203220, "id": 16}, {"city": "", "name": "tusbic", "url": "http://api.citybik.es/tusbic.json", "radius": 20000, "lat": 43471106, "lng": -3800586, "id": 17}, {"city": "", "name": "goteborg", "url": "http://api.citybik.es/goteborg.json", "radius": 20000, "lat": 57702955, "lng": 11980210, "id": 18}, {"city": "", "name": "citycycle", "url": "http://api.citybik.es/citycycle.json", "radius": 20000, "lat": -27471297, "lng": 153022570, "id": 19}, {"city": "", "name": "wien", "url": "http://api.citybik.es/wien.json", "radius": 20000, "lat": 48192986, "lng": 16398464, "id": 20}, {"city": "", "name": "mulhouse", "url": "http://api.citybik.es/mulhouse.json", "radius": 20000, "lat": 47752433, "lng": 7337387, "id": 21}, {"city": "", "name": "levelo", "url": "http://api.citybik.es/levelo.json", "radius": 20000, "lat": 43288726, "lng": 5394038, "id": 22}, {"city": "", "name": "velam", "url": "http://api.citybik.es/velam.json", "radius": 20000, "lat": 49897290, "lng": 2297864, "id": 23}, {"city": "", "name": "velov", "url": "http://api.citybik.es/velov.json", "radius": 20000, "lat": 45757864, "lng": 4868392, "id": 24}, {"city": "", "name": "bixi", "url": "http://api.citybik.es/bixi.json", "radius": 20000, "lat": 45531949, "lng": -73553263, "id": 25}, {"city": "", "name": "melbourne", "url": "http://api.citybik.es/melbourne.json", "radius": 20000, "lat": -37818306, "lng": 144945923, "id": 26}, {"city": "", "name": "girocleta", "url": "http://api.citybik.es/girocleta.json", "radius": 20000, "lat": 41974679, "lng": 2820382, "id": 27}, {"city": "", "name": "capitalbikeshare", "url": "http://api.citybik.es/capitalbikeshare.json", "radius": 20000, "lat": 38912600, "lng": -77013500, "id": 28}, {"city": "", "name": "decobike", "url": "http://api2.citybik.es/decobike.json", "radius": 20000, "lat": 25779274, "lng": -80140010, "id": 29}, {"city": "", "name": "niceride", "url": "http://api.citybik.es/niceride.json", "radius": 20000, "lat": 44975641, "lng": -93272185, "id": 30}, {"city": "", "name": "denver", "url": "http://api.citybik.es/denver.json", "radius": 20000, "lat": 39727670, "lng": -104990850, "id": 31}, {"city": "", "name": "desmoines", "url": "http://api.citybik.es/desmoines.json", "radius": 20000, "lat": 41587640, "lng": -93626200, "id": 32}, {"city": "", "name": "sanantonio", "url": "http://api.citybik.es/sanantonio.json", "radius": 20000, "lat": 29415950, "lng": -98489740, "id": 33}, {"city": "", "name": "tobike", "url": "http://api.citybik.es/tobike.json", "radius": 20000, "lat": 45066238, "lng": 7682222, "id": 34}, {"city": "", "name": "hawaii", "url": "http://api.citybik.es/hawaii.json", "radius": 20000, "lat": 21393260, "lng": -157744630, "id": 35}, {"city": "", "name": "bicikelj", "url": "http://api.citybik.es/bicikelj.json", "radius": 20000, "lat": 46057421, "lng": 14510265, "id": 36}, {"city": "", "name": "boulder", "url": "http://api.citybik.es/boulder.json", "radius": 20000, "lat": 40031540, "lng": -105246110, "id": 37}, {"city": "", "name": "bikemi", "url": "http://api.citybik.es/bikemi.json", "radius": 20000, "lat": 45452311, "lng": 9177489, "id": 38}, {"city": "", "name": "mejorenbici", "url": "http://api.citybik.es/mejorenbici.json", "radius": 20000, "lat": -34609829, "lng": -58389289, "id": 39}, {"city": "", "name": "palma", "url": "http://api.citybik.es/palma.json", "radius": 20000, "lat": 39581315, "lng": 2655279, "id": 40}, {"city": "", "name": "bicileon", "url": "http://api.citybik.es/bicileon.json", "radius": 20000, "lat": 42592583, "lng": -5571308, "id": 41}, {"city": "", "name": "bysykkel", "url": "http://api2.citybik.es/bysykkel.json", "radius": 20000, "lat": 59910696, "lng": 10736818, "id": 42}, {"city": "", "name": "rome", "url": "http://api.citybik.es/rome.json", "radius": 20000, "lat": 41900074, "lng": 12473173, "id": 43}, {"city": "", "name": "venice", "url": "http://api.citybik.es/venice.json", "radius": 20000, "lat": 45479978, "lng": 12230497, "id": 44}, {"city": "", "name": "tigullionbike", "url": "http://api.citybik.es/tigullionbike.json", "radius": 20000, "lat": 44275810, "lng": 9397130, "id": 45}, {"city": "", "name": "toronto", "url": "http://api2.citybik.es/toronto.json", "radius": 20000, "lat": 43653743, "lng": -79388820, "id": 46}, {"city": "", "name": "madison", "url": "http://api.citybik.es/madison.json", "radius": 20000, "lat": 43067540, "lng": -89414030, "id": 47}, {"city": "", "name": "vlille", "url": "http://api.citybik.es/vlille.json", "radius": 20000, "lat": 50646200, "lng": 3053500, "id": 48}, {"city": "", "name": "citibikenyc", "url": "http://api.citybik.es/citibikenyc.json", "radius": 20000, "lat": 40713126, "lng": -73984844, "id": 49}, {"city": "", "name": "ottawa", "url": "http://api.citybik.es/ottawa.json", "radius": 20000, "lat": 45428997, "lng": -75691066, "id": 50}, {"city": "", "name": "broward", "url": "http://api.citybik.es/broward.json", "radius": 20000, "lat": 26167390, "lng": -80100320, "id": 51}, {"city": "", "name": "charlotte", "url": "http://api.citybik.es/charlotte.json", "radius": 20000, "lat": 35224430, "lng": -80839780, "id": 52}, {"city": "", "name": "fortworth", "url": "http://api.citybik.es/fortworth.json", "radius": 20000, "lat": 32750990, "lng": -97325390, "id": 53}, {"city": "", "name": "greenville", "url": "http://api.citybik.es/greenville.json", "radius": 20000, "lat": 34845010, "lng": -82404480, "id": 54}, {"city": "", "name": "houston", "url": "http://api.citybik.es/houston.json", "radius": 20000, "lat": 29752870, "lng": -95370640, "id": 55}, {"city": "", "name": "nashville", "url": "http://api.citybik.es/nashville.json", "radius": 20000, "lat": 36162520, "lng": -86774260, "id": 56}, {"city": "", "name": "spartanburg", "url": "http://api.citybik.es/spartanburg.json", "radius": 20000, "lat": 34947470, "lng": -81921740, "id": 57}, {"city": "", "name": "kansas", "url": "http://api.citybik.es/kansas.json", "radius": 20000, "lat": 39110540, "lng": -94580740, "id": 58}, {"city": "", "name": "omaha", "url": "http://api.citybik.es/omaha.json", "radius": 20000, "lat": 41244940, "lng": -96014280, "id": 59}, {"city": "", "name": "divvybikes", "url": "http://api.citybik.es/divvybikes.json", "radius": 20000, "lat": 41896802, "lng": -87635638, "id": 60}, {"city": "", "name": "albacete", "url": "http://api2.citybik.es/albacete.json", "radius": 20000, "lat": 38979603, "lng": -1852836, "id": 61}, {"city": "", "name": "montilla", "url": "http://api2.citybik.es/montilla.json", "radius": 20000, "lat": 37591420, "lng": -4634027, "id": 62}, {"city": "", "name": "puertobike", "url": "http://api2.citybik.es/puertobike.json", "radius": 20000, "lat": 38705472, "lng": -4108123, "id": 63}, {"city": "", "name": "ciudad_real", "url": "http://api2.citybik.es/ciudad_real.json", "radius": 20000, "lat": 38968318, "lng": -3932290, "id": 64}, {"city": "", "name": "villaquilambre", "url": "http://api2.citybik.es/villaquilambre.json", "radius": 20000, "lat": 42635616, "lng": -5535822, "id": 65}, {"city": "", "name": "cieza", "url": "http://api2.citybik.es/cieza.json", "radius": 20000, "lat": 38237147, "lng": -1422581, "id": 66}, {"city": "", "name": "san_pedro", "url": "http://api2.citybik.es/san_pedro.json", "radius": 20000, "lat": 37830619, "lng": -786961, "id": 67}, {"city": "", "name": "almunecar", "url": "http://api2.citybik.es/almunecar.json", "radius": 20000, "lat": 36735789, "lng": -3688828, "id": 68}, {"city": "", "name": "las_palmas", "url": "http://api2.citybik.es/las_palmas.json", "radius": 20000, "lat": 28109980, "lng": -15416214, "id": 69}, {"city": "", "name": "redondela", "url": "http://api2.citybik.es/redondela.json", "radius": 20000, "lat": 42289405, "lng": -8610783, "id": 70}, {"city": "", "name": "badajoz", "url": "http://api2.citybik.es/badajoz.json", "radius": 20000, "lat": 38887061, "lng": -6999322, "id": 71}, {"city": "", "name": "soria", "url": "http://api2.citybik.es/soria.json", "radius": 20000, "lat": 41770736, "lng": -2468405, "id": 72}, {"city": "", "name": "salamanca", "url": "http://api2.citybik.es/salamanca.json", "radius": 20000, "lat": 40970589, "lng": -5674858, "id": 73}, {"city": "", "name": "bicielx", "url": "http://api2.citybik.es/bicielx.json", "radius": 20000, "lat": 38264171, "lng": -695433, "id": 74}, {"city": "", "name": "villarreal", "url": "http://api2.citybik.es/villarreal.json", "radius": 20000, "lat": 39934446, "lng": -104581, "id": 75}, {"city": "", "name": "lalin", "url": "http://api2.citybik.es/lalin.json", "radius": 20000, "lat": 42656756, "lng": -8113497, "id": 76}, {"city": "", "name": "aranda_de_duero", "url": "http://api2.citybik.es/aranda_de_duero.json", "radius": 20000, "lat": 41658614, "lng": -3692539, "id": 77}, {"city": "", "name": "a_rua", "url": "http://api2.citybik.es/a_rua.json", "radius": 20000, "lat": 42389167, "lng": -7115135, "id": 78}, {"city": "", "name": "ubeda", "url": "http://api2.citybik.es/ubeda.json", "radius": 20000, "lat": 38016477, "lng": -3374809, "id": 79}, {"city": "", "name": "via_verde", "url": "http://api2.citybik.es/via_verde.json", "radius": 20000, "lat": 42758640, "lng": -8946980, "id": 80}, {"city": "", "name": "novelda", "url": "http://api2.citybik.es/novelda.json", "radius": 20000, "lat": 38380242, "lng": -766225, "id": 81}, {"city": "", "name": "aranjuez", "url": "http://api2.citybik.es/aranjuez.json", "radius": 20000, "lat": 40034711, "lng": -3618158, "id": 82}, {"city": "", "name": "paiporta", "url": "http://api2.citybik.es/paiporta.json", "radius": 20000, "lat": 39432215, "lng": -418575, "id": 83}, {"city": "", "name": "antequera", "url": "http://api2.citybik.es/antequera.json", "radius": 20000, "lat": 37021608, "lng": -4562160, "id": 84}, {"city": "", "name": "el_campello", "url": "http://api2.citybik.es/el_campello.json", "radius": 20000, "lat": 38427393, "lng": -389349, "id": 85}, {"city": "", "name": "don_benito_villanueva", "url": "http://api2.citybik.es/don_benito_villanueva.json", "radius": 20000, "lat": 38955625, "lng": -5868969, "id": 86}, {"city": "", "name": "puerto_lumbreras", "url": "http://api2.citybik.es/puerto_lumbreras.json", "radius": 20000, "lat": 37561408, "lng": -1808719, "id": 87}, {"city": "", "name": "san_javier", "url": "http://api2.citybik.es/san_javier.json", "radius": 20000, "lat": 37770612, "lng": -858953, "id": 88}, {"city": "", "name": "o_barco", "url": "http://api2.citybik.es/o_barco.json", "radius": 20000, "lat": 42415789, "lng": -6984611, "id": 89}, {"city": "", "name": "baeza", "url": "http://api2.citybik.es/baeza.json", "radius": 20000, "lat": 37996399, "lng": -3464341, "id": 90}, {"city": "", "name": "pinto", "url": "http://api2.citybik.es/pinto.json", "radius": 20000, "lat": 40247715, "lng": -3692361, "id": 91}, {"city": "", "name": "alhama_de_murcia", "url": "http://api2.citybik.es/alhama_de_murcia.json", "radius": 20000, "lat": 37852744, "lng": -1413188, "id": 92}, {"city": "", "name": "bicio", "url": "http://api2.citybik.es/bicio.json", "radius": 20000, "lat": 40944752, "lng": -4121697, "id": 93}, {"city": "", "name": "palencia", "url": "http://api2.citybik.es/palencia.json", "radius": 20000, "lat": 42019451, "lng": -4530283, "id": 94}, {"city": "", "name": "blanca", "url": "http://api2.citybik.es/blanca.json", "radius": 20000, "lat": 38178080, "lng": -1376971, "id": 95}, {"city": "", "name": "sant_joan", "url": "http://api2.citybik.es/sant_joan.json", "radius": 20000, "lat": 38389008, "lng": -435455, "id": 96}, {"city": "", "name": "guadalajara", "url": "http://api2.citybik.es/guadalajara.json", "radius": 20000, "lat": 40636511, "lng": -3168567, "id": 97}, {"city": "", "name": "priego_de_cordoba", "url": "http://api2.citybik.es/priego_de_cordoba.json", "radius": 20000, "lat": 37441353, "lng": -4211297, "id": 98}, {"city": "", "name": "jaen", "url": "http://api2.citybik.es/jaen.json", "radius": 20000, "lat": 37767536, "lng": -3787603, "id": 99}, {"city": "", "name": "universidad_de_granada", "url": "http://api2.citybik.es/universidad_de_granada.json", "radius": 20000, "lat": 37181394, "lng": -3606936, "id": 100}, {"city": "", "name": "talavera", "url": "http://api2.citybik.es/talavera.json", "radius": 20000, "lat": 39959821, "lng": -4833009, "id": 101}, {"city": "", "name": "mula", "url": "http://api2.citybik.es/mula.json", "radius": 20000, "lat": 38038892, "lng": -1496061, "id": 102}, {"city": "", "name": "vinaros", "url": "http://api2.citybik.es/vinaros.json", "radius": 20000, "lat": 40478680, "lng": 476291, "id": 103}, {"city": "", "name": "vigo", "url": "http://api2.citybik.es/vigo.json", "radius": 20000, "lat": 42239035, "lng": -8725371, "id": 104}, {"city": "", "name": "velov", "url": "http://api2.citybik.es/velov.json", "radius": 20000, "lat": 45764409, "lng": 4836044, "id": 105}]
require(shiny)
# Thanks to @jcheng5
# Data is common across all sessions. So it makes sense to do the operation
# just once. Moroever, the map is regenerated only when data changes.
# sharedValues <- reactiveValues()
# observe({
# invalidateLater(600000, NULL) # ten minutes
# message('Fetching Data')
# sharedValues$data <- getData('boulder')
# })
shinyServer(function(input, output, session){
output$map_container <- renderMap({
plotData(getData(input$network), input$network)
})
})
require(shiny)
require(rCharts)
shinyUI(bootstrapPage(
tags$style('#network {position: absolute; bottom: 10px; right: 0; z-index: 10;}'),
selectInput('network', '', sort(names(networks)), 'citibikenyc'),
mapOutput('map_container')
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment