This R package makes it easy to integrate and control Leaflet maps in R.
- Interactive panning/zooming
- Compose maps using arbitrary combinations of:
- Map tiles
- Create maps right from the R console or RStudio
- Embed maps in knitr/R Markdown documents and Shiny apps
- Easily render
Spatialobjects from the
sppackage, or data frames with latitude/longitude columns
- Use map bounds and mouse events to drive Shiny logic
To install this R package, run this command at your R prompt:
install.packages("leaflet") # to install the development version from Github, run # devtools::install_github("rstudio/leaflet")
You create a Leaflet map with these basic steps:
- Create a map widget by calling
- Add layers (i.e., features) to the map by using layer functions (e.g.
addPolygons) to modify the map widget.
- Repeat step 2 as desired.
- Print the map widget to display it.
Here’s a basic example:
library(leaflet) m <- leaflet() %>% addTiles() %>% # Add default OpenStreetMap map tiles addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R") m # Print the map
In case you’re not familiar with the magrittr pipe operator (
%>%), here is the equivalent without using pipes:
m <- leaflet() m <- addTiles(m) m <- addMarkers(m, lng=174.768, lat=-36.852, popup="The birthplace of R") m
We highly recommend that you proceed to The Map Widget page before exploring the rest of this site, as it describes common idioms we’ll use throughout the examples on the other pages.
Although we have tried to provide an R-like interface to Leaflet, you may want to check out the API documentation of Leaflet occasionally when the meanings of certain parameters are not clear to you.