LEAFLET map in R Shiny

LEAFLET map in R Shiny

A simple example of a map created in R, using the Shiny and Leaflet packages.

The code below:

  • loads a base map which the user can scroll and zoom
  • sets an initial location center and zoom
  • adds a placemarker with an information popup when the marker is clicked
  • a reset button returns the map to the original view.

See links below for:

Prerequisites
  • R and RStudio;
  • R Packages Shiny, Leaflet
Instructions
  1. Copy the code below and paste into a new file in RStudio;
  2. Save as an R Markdown file (extension .rmd);
  3. Install packages Shiny and Leaflet if not already installed; and
  4. Run in R Studio. The markdown file produces an html document which can be loaded into a browser.
Code without comments (see code with comments below)
---
title: "Leaflet map in R Shiny"
runtime: shiny
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)

library(shiny)
library(leaflet)

```

```{r map}

inputPanel(actionButton
(inputId="resetMap",
	label="Reset view", 
	style="color: #fff; background-color: #990000; border-style: solid; border-color: #999999; margin: 5px")
	 )

leafletOutput("londonMap",
	height=600,
	width="100%")


output$londonMap <- renderLeaflet({
theMap <- leaflet(options = leafletOptions(zoomSnap=0.1)) %>%
addTiles() %>%
setView(lng=-0.128034, lat=51.508047, zoom = 12) %>% 
addMarkers(lng=-0.128034, lat=51.508047,popup="Trafalgar Square, London") 
})

observeEvent(input$resetMap, 
	{leafletProxy("londonMap") %>%
setView(lng=-0.128034, lat=51.508047, zoom = 12)
})


```

image-map
Screenshot of Leaflet map produced in R Shiny
References for the code – see also code with comments below

Functions in Shiny
Functions in Leaflet
Functions in base R and R markdown
Custom variables

These are the custom variables used in the code which you can replace with your own names:

  • resetMap (id for the action button)
  • londonMap (name for the ‘reactive’ map output)
  • theMap (name for the actual map).
Code with comments
---
title: "Leaflet map in R Shiny"
runtime: shiny
output: html_document
---

```{r setup, include=FALSE} #include=TRUE would include the
knitr::opts_chunk$set(echo = FALSE)

library(shiny)
library(leaflet)

```

```{r map}
# create the button to reset the map view
inputPanel(actionButton
(inputId="resetMap",
	label="Reset view", 
	style="color: #fff; background-color: #990000; border-style: solid; border-color: #999999; margin: 5px")
	 )
# display the map
leafletOutput("londonMap",
	height=600,
	width="100%")

# create the output which will be displayed in the page in the 'leafletOutput' function above
output$londonMap <- renderLeaflet({
theMap <- leaflet(options = leafletOptions(zoomSnap=0.1)) %>%
addTiles() %>%
setView(lng=-0.128034, lat=51.508047, zoom = 12) %>% 
addMarkers(lng=-0.128034, lat=51.508047,popup="Trafalgar Square, London") 
})

# the event which goes with the actionButton above (id name is 'resetMap')
# when the button is clicked, the map will return to the original center and zoom
observeEvent(input$resetMap, 
	{leafletProxy("londonMap") %>%
setView(lng=-0.128034, lat=51.508047, zoom = 12)
})


```
Links to resources