- The software (tools of the trade)
- Context (motivations)
- Building the National Propensity to Cycle Tool (NPCT)
- Sustainable transport planning with R
- Esperanzas y sueños
University of Lancaster 21st August 2015
See the free, open source, online tutorial github.com/robinlovelace/Creating-maps-in-R.
Can be installed and loaded in 6 lines of code:
pkgs <- c("devtools", "shiny", "rgdal", "rgeos", "ggmap") # official packages
install.packages(pkgs)
library(devtools) # enables installation of leaflet
gh_pkgs <- c("rstudio/leaflet", "robinlovelace/stplanr")
install_github(gh_pkgs) # install packages on github
lapply(c(pkgs, "leaflet", "stplanr"), library, character.only = T) # load all
RStudio Desktop is highly recommended for Shiny development.
shiny is a framework for creating online interactive data visualisation 'apps'.
server.R and ui.R files.For maps
Build's on R's existing strengths
Flexibility
Not so good for
# type this to find out! runExample()
## Valid examples are "01_hello", "02_text", "03_reactivity", "04_mpg", "05_sliders", "06_tabsets", "07_widgets", "08_html", "09_upload", "10_download", "11_timer"
An R interface to the Leaflet JavaScript library, compatible with Shiny.
cent <- geocode("Girona")
leaflet() %>%
addTiles() %>%
addCircleMarkers(data = cent)
runApp("~/repos/pct-shiny/master/", launch.browser = T)
Key components:
|-- master | |-- master.R | |-- server.R | |-- ui.R | `-- pct-shiny-funs.R |-- manchester | |-- server.R (links to ../master.R) | `-- ui.R (links to ../ui.R) `-- README.Rmd
runApp("~/repos/pct-shiny/norwich/", launch.browser = T)
runApp("~/repos/pct-shiny/cambridge/", launch.browser = T)
runApp("~/repos/pct-shiny/manchester/", launch.browser = T)
start_time <- Sys.time() # for timing the script
la <- "manchester" # Name of the local authority
dir.create(paste0("pct-data/", la))
# ... 200 + lines of code here!
# ...
end_time <- Sys.time()
end_time - start_time
kable(head(flow[1:3]))
| Area.of.residence | Area.of.workplace | All | |
|---|---|---|---|
| 920573 | E02002361 | E02002361 | 109 |
| 920575 | E02002361 | E02002363 | 38 |
| 920578 | E02002361 | E02002367 | 10 |
| 920582 | E02002361 | E02002371 | 44 |
| 920587 | E02002361 | E02002377 | 34 |
| 920591 | E02002361 | E02002382 | 7 |
library(stplanr) flowlines <- gFlow2line(flow = flow, zones = cents) plot(flowlines)
This code is not run - you need your own API key
example(gLines2CyclePath) data(package = "stplanr", "flowlines") ?route_cyclestreet plot(flowlines) Sys.setenv(CYCLESTREET = 'eccbf612-214e-437d-8b73-06bdf9e68731') routes_fast <- gLines2CyclePath(flowlines) routes_slow <- gLines2CyclePath(flowlines, "quietest") lines(routes_fast, col = "red") lines(routes_slow, col = "green")
lanc_2_lds <- route_graphhopper(from = "University of Lancaster", to = "Leeds")
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=University+of+Lancaster&sensor=false ## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Leeds&sensor=false
## [1] "The request sent was: https://graphhopper.com/api/1/route?point=54.0103942%2C-2.7877294&point=53.8007554%2C-1.5490774&vehicle=bike&locale=en-US&debug=true&points_encoded=false&key=eccbf612-214e-437d-8b73-06bdf9e6877e&elevation=true"
# nominatim::address_lookup("University of Lancaster")
# for online mapping
# leaflet() %>% addTiles() %>% addPolylines(data = lanc_2_lds)
plot(lanc_2_lds)
lanc_2_lds@data
## time dist climb ## 1 461.5743 111422.9 2075.8
data("routes_fast","routes_slow")
flowlines <- spTransform(flowlines, CRS("+init=epsg:4326"))
plot(flowlines)
lines(routes_fast, col = "red")
lines(routes_slow, col = "green")
gLines2CyclePath(l, plan = "fastest")
This problem was solved by Barry Rowlingson on gis.stackexchange: