leaflet Exercises in R: 20 Practice Problems

Twenty practice problems on leaflet in R: markers, layers, popups, tiles, choropleths, geocoding. Hidden solutions.

RRun this once before any exercise
library(leaflet) library(dplyr) library(htmlwidgets) library(sf)

  

Exercise 1: Blank map

Difficulty: Beginner.

Show solution
RInteractive R
leaflet() |> addTiles()

  

Exercise 2: Set view

Difficulty: Beginner.

Show solution
RInteractive R
leaflet() |> addTiles() |> setView(lng = -74, lat = 40.7, zoom = 12)

  

Exercise 3: Add marker

Difficulty: Beginner.

Show solution
RInteractive R
leaflet() |> addTiles() |> addMarkers(-74, 40.7, popup = "NYC")

  

Exercise 4: Many markers

Difficulty: Intermediate.

Show solution
RInteractive R
df <- data.frame(lon = c(-74, -118, -87), lat = c(40.7, 34.0, 41.9), name = c("NYC","LA","Chicago")) leaflet(df) |> addTiles() |> addMarkers(~lon, ~lat, popup = ~name)

  

Exercise 5: Circle markers

Difficulty: Intermediate.

Show solution
RInteractive R
df <- data.frame(lon = c(-74, -118), lat = c(40.7, 34.0)) leaflet(df) |> addTiles() |> addCircleMarkers(~lon, ~lat, radius = 5)

  

Exercise 6: Color by value

Difficulty: Advanced.

Show solution
RInteractive R
df <- data.frame(lon = c(-74, -118, -87), lat = c(40.7, 34.0, 41.9), score = c(85, 70, 92)) pal <- colorNumeric("Blues", df$score) leaflet(df) |> addTiles() |> addCircleMarkers(~lon, ~lat, color = ~pal(score), radius = 8, fillOpacity = 0.8)

  

Exercise 7: Tile providers

Difficulty: Intermediate.

Show solution
RInteractive R
leaflet() |> addProviderTiles("CartoDB.Positron")

  

Exercise 8: Multiple tile layers

Difficulty: Advanced.

Show solution
RInteractive R
leaflet() |> addTiles(group = "OSM") |> addProviderTiles("CartoDB.Positron", group = "Carto") |> addLayersControl(baseGroups = c("OSM","Carto"))

  

Exercise 9: Polygons

Difficulty: Advanced.

Show solution
RInteractive R
# Simple square polygon poly <- list(list(c(-74.1, 40.7), c(-74.0, 40.7), c(-74.0, 40.8), c(-74.1, 40.8), c(-74.1, 40.7))) leaflet() |> addTiles() |> addPolygons(data = sf::st_polygon(poly) |> sf::st_sfc(crs = 4326))

  

Exercise 10: Polylines

Difficulty: Intermediate.

Show solution
RInteractive R
leaflet() |> addTiles() |> addPolylines(lng = c(-74, -73.9, -73.8), lat = c(40.7, 40.75, 40.8))

  

Exercise 11: Popups with HTML

Difficulty: Intermediate.

Show solution
RInteractive R
leaflet() |> addTiles() |> addMarkers(-74, 40.7, popup = "<b>NYC</b><br>Pop ~ 8.4M")

  

Exercise 12: Labels (tooltips)

Difficulty: Intermediate.

Show solution
RInteractive R
leaflet() |> addTiles() |> addMarkers(-74, 40.7, label = "Hover me")

  

Exercise 13: Choropleth (concept)

Difficulty: Advanced.

Show solution
RInteractive R
# nc <- sf::st_read(system.file("shape/nc.shp", package="sf")) # pal <- colorNumeric("Reds", nc$AREA) # leaflet(nc) |> addTiles() |> addPolygons(fillColor = ~pal(AREA), fillOpacity = 0.7)

  

Exercise 14: Mini-map

Difficulty: Advanced.

Show solution
RInteractive R
leaflet() |> addTiles() |> addMiniMap()

  

Exercise 15: Scale bar

Difficulty: Intermediate.

Show solution
RInteractive R
leaflet() |> addTiles() |> addScaleBar(position = "bottomleft")

  

Exercise 16: Measure widget

Difficulty: Intermediate.

Show solution
RInteractive R
leaflet() |> addTiles() |> addMeasure(primaryLengthUnit = "kilometers")

  

Exercise 17: Layer groups + toggle

Difficulty: Advanced.

Show solution
RInteractive R
leaflet() |> addTiles() |> addMarkers(-74, 40.7, group = "NYC") |> addMarkers(-118, 34.0, group = "LA") |> addLayersControl(overlayGroups = c("NYC","LA"))

  

Exercise 18: Legend

Difficulty: Advanced.

Show solution
RInteractive R
df <- data.frame(lon = c(-74,-118), lat = c(40.7, 34.0), score = c(85, 70)) pal <- colorNumeric("Blues", df$score) leaflet(df) |> addTiles() |> addCircleMarkers(~lon, ~lat, color = ~pal(score)) |> addLegend("bottomright", pal = pal, values = ~score, title = "Score")

  

Exercise 19: Cluster markers

Difficulty: Advanced.

Show solution
RInteractive R
df <- data.frame(lon = runif(100, -74.1, -73.9), lat = runif(100, 40.6, 40.8)) leaflet(df) |> addTiles() |> addMarkers(~lon, ~lat, clusterOptions = markerClusterOptions())

  

Exercise 20: Save to HTML

Difficulty: Intermediate.

Show solution
RInteractive R
m <- leaflet() |> addTiles() |> addMarkers(-74, 40.7) htmlwidgets::saveWidget(m, "map.html")

  

What to do next

  • Data-Visualization-Exercises (shipped), broader viz.
  • Shiny-Exercises (shipped), embed leaflet in Shiny apps.