It has been possible for some years to launch a web map from within R. A number of packages for doing this are available, including:

In this tutorial we use the new RStudio-supported leaflet R package. We use this package, an R interface to the JavaScript mapping library of the same name because:

The best tutorial resource I have found on leaflet is this vignette by Joe Cheng and Yihui Xie. Building on their excellent description, this article explains some of the fundamentals of the package.

Installing leaflet

Because leaflet is new, it’s not yet on CRAN. Even when it does appear, installing from github may be a good idea, to ensure you have access to the latest features and bug fixes. Here’s how:

# Install leaflet package
if(!require(leaflet)) install_github("rstudio/leaflet") 

A first web map with leaflet

To create an interactive web map with leaflet is incredibly easy. Having installed the package try this single line of code:

# Plot a default web map (brackets display the result)
(m <- leaflet() %>% addTiles()) 
img <- readPNG("~/repos/Creating-maps-in-R/figure//shiny_world.png")

Basic leaflet map in R

Adding basic features with %>%

Adding basic features to your webmap is easy. The %>% ‘pipe’ operator used extensively in dplyr and developed for the maggrittr package means we can finally escape from dozens of nested bracketted commands. (If you use RStudio, I suggest trying the new shortcut Ctl+Shift+M to produce this wonderful operator.) leaflet settings and functionality can thus be added sequentially, without requiring any additional brackets!

To add a location to the map m, for example, we can simply pipe m into the function setView():

m %>% setView(lng = -1.5, lat = 53.4, zoom = 10) # set centre and extent of map

This way we can gradually add elements to our map, one-by-one:

(m2 <- m %>%
  setView(-1.5, 53.4, 10) %>% # map location
  addMarkers(-1.4, 53.5) %>% # add a marker
  addPopups(-1.6, 53.3, popup = "Hello Sheffield!") %>% # popup
  # add som circles:
  addCircles(color = "black", runif(90, -2, -1), runif(90, 53, 54), runif(90, 10, 500)))

Adding data

In the previous example, we added some random data that we created locally, inside the function call. How do we use real, large datasets in leaflet? The package provides 3 options:

Let’s try adding a bicycle route, one that I followed with some friends to move from Sheffield to my current home of Leeds. First download some data:

url = ""
download.file(url, destfile = "shef2leeds.gpx", method = "wget")

Now we can load this as a SpatialLinesDataFrame and display in leaflet:

shef2leeds <- readOGR("shef2leeds.gpx", layer = "tracks")
m2 %>%
  setView(-1.5, 53.4, 9) %>% # map location
  addPolylines(data = shef2leeds, color = "red", weight = 4)

Note in the above example that we had to use the argument data = to refer to our spatial object: it cannot simply be inserted without specifying what it is. The data argument can also be placed inside the initial leaflet() function call.

That was quite a painless process that would many more lines of code if you were to use JavaScript. But not as painless as the bicycle trip itself, which involved fewer lines of code still: 0! This can be seen in the following video.

Shiny integration

leaflet is developed by the same team who develop shiny so the two are well integrated. Below is a very simple example, modified slightly from the package’s vignette:

  ui = fluidPage(leafletOutput('myMap')),
  server = function(input, output) {
    # download and load data
    url = ""
    download.file(url, destfile = "shef2leeds.gpx", method = "wget", )
    shef2leeds <- readOGR("shef2leeds.gpx", layer = "tracks")
    map = leaflet() %>% addTiles() %>% setView(-1.5, 53.4, 9) %>% 
      addPolylines(data = shef2leeds, color = "red", weight = 4)
    output$myMap = renderLeaflet(map)


Clearly leaflet is a powerful and flexible R package. If I were to offer one critique, it would be that I could find no easy way to allow the user to query the data objects loaded. plotly, for example, provides a description of any visual object the user clicks on. The datashine commuter visualisation, for example allows users to click on any point, resulting in a burst of lines emenating from it. This would also be possible in leaflet/shiny, but the best implementation is not immediately clear, to me at least!

The wider context of this article is the pressing need for better transport planning decision making, to enable a transition away from fossil fuels. To this end, the ‘propensity to cycle’ project, funded by the UK’s Department for Transport, seeks to create an interactive tool to identify where new bicycle paths are most needed. There are clearly many other uses for R’s leaflet package: what will you use it for? Let me know at @robinlovelace.