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.
This exercise is part of the course
Interactive Maps with leaflet in R
Exercise instructions
- Load the
leaflet
library. - Call the
leaflet()
function. - Pipe the output of
leaflet()
intoaddTiles()
. - Experiment with zooming and panning your first interactive web map.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Load the leaflet library
library(___)
# Create a leaflet map with default map tile using addTiles()
___() %>%
___()