Session Ready
Exercise

Creating an Interactive Web Map

Similar to the packages in the tidyverse, the leaflet package makes use of the pipe operator (i.e., %>%) from the magrittr package to chain function calls together. This means we can pipe the result of one function into another without having to store the intermediate output in an object. For example, one way to find every car in the mtcars data set with a mpg >= 25 is to pipe the data through a series of functions.

mtcars  %>% 
    mutate(car = rownames(.))  %>% 
    select(car, mpg)  %>% 
    filter(mpg >= 25)  

To create a web map in R, you will chain together a series of function calls using the %>% operator. Our first function leaflet() will initialize the htmlwidget then we will add a map tile using the addTiles() function.

Instructions
100 XP
  • Load the leaflet library.
  • Call the leaflet() function.
  • Pipe the output of leaflet() into addTiles().
  • Experiment with zooming and panning your first interactive web map.