r5r: Rapid Realistic Routing with R5 in R


I’m glad to announce r5r, an R package for rapid realistic routing on multimodal transport networks (walk, bike, public transport and car). The package has been developed by my team and I as part of the Access to Opportunities Project because it’s fun we wanted to make transport modeling and accessibility analysis easier and faster.

r5r provides a simple and friendly interface to R5, a powerful routing engine developed by Conveyal. The r5r package is a simple way to run R5 locally, what allows users to generate detailed routing analysis or calculate travel time matrices using seamless parallel computing.

The package has very low data requirements, which means you can use it to analyze pretty much any city, region or country. It only requires data on street networks from OpenStreetMap. It does allow for multimodal transport routing if public transport data is provided in the standard GTFS format. The package is also easily scalable allowing for fast computation at either the city or country level.

I’ll give a quick overview of the package in this post. For a detailed demonstration of r5r, see in this intro Vignette.


To use r5r, you need to have Java SE Development Kit version >= 11.0.8 installed on your computer. No worries, you don’t have to pay for it.

# install package from CRAN

# allocate some RAM memory to Java, 2GB in this case
options(java.parameters = "-Xmx2G")

# load package

Demonstration on sample data

This quick demo shows how you can use r5r in three simple steps. The package includes a small sample data set of the public transport and Open Street Map networks of São Paulo (Brazil), which I’ll be using here.

In this example, we will be calculating the travel time by public transport between every possible pair of origins and destinations in the sample data.

1) Build a routable transport network

You only need to point to the local directory with your data. The function will automatically download and store locally an R5 Jar file (Jar file is downloaded only once), and builds a multimodal transport network given an OSM street network in .pbf format (mandatory) and one or more public transport networks in GTFS.zip format (optional).

path <- system.file("extdata/spo", package = "r5r")
r5r_core <- setup_r5(data_path = path, verbose = FALSE)

2) Set trip parameters

# Load origin/destination points
points <- read.csv(system.file("extdata/spo/spo_hexgrid.csv", package = "r5r"))

# Set transport mode
mode <- c("transit")

# Set trip departure time
departure_datetime <- as.POSIXct("13-05-2019 10:00:00", format = "%d-%m-%Y %H:%M:%S")
time_window <- 60 # minutes
percentiles <- 50  # 50th percentile

We all know travel times might vary at different times of the day. To account for this variation, what we do in this example is to compute several travel time matrices with multiple departure times within a 60-minute window and retrieve the median travel time within that window.

3) Compute travel time matrix

Now we only need to compute the travel times, which is blazing fast thanks to R5.

ttm <- travel_time_matrix(r5r_core = r5r_core,
                          origins = points,
                          destinations = points,
                          mode = mode,
                          departure_datetime = departure_datetime,
                          time_window = time_window,
                          percentiles = percentiles)

The output is a data.table that brings time estimates (in minutes), and it looks like this:

>             fromId            toId travel_time
> 1: 89a8100c603ffff 89a8100c603ffff           0
> 2: 89a8100c603ffff 89a8100c617ffff          13
> 3: 89a8100c603ffff 89a8100c60fffff           6
> 4: 89a8100c603ffff 89a8100c607ffff          11
> 5: 89a8100c603ffff 89a8100c6abffff          20

In this example, we’ve calculated the median travel time within a one hour-window for over 104 thousand origin/destination pairs, what took 38 seconds in my laptop. When using a single departure time, it takes less than 4 seconds.

r5r Allows for other routing parameters and it also brings the detailed_itineraries() function, which returns detailed information on transport mode, travel time, walk distance, geometry etc for each trip section of multiple alternative routes between origin/destination pairs. More details can be found on the package webpage.

ps. The package has been published on CRAN for a few months and we already have over 1600 downloads, but only now I managed to have some time to share about the package here on my website.

comments powered by Disqus