Search code examples
rleafletknitrr-markdownr-mapview

Synchronizing two leaflet maps in R / Rmarkdown


JS leaflet allows two maps to be synchronized. See an example of synchronized leaflet maps here.

I would like to implement synchronized leaflet maps in R and more specifially in Rmarkdown/knitr.

Preferably, the maps should shown next to each other horizontally (just like in the example).

Here is a minimal Rmarkdown (.Rmd) example of two maps I would like to sync. The solution does not have to be based on the the mapview package. Any solution is welcome really (-:

---
title: "How to sync 2 leaflet maps"
author: "me"
date: "2 April 2016"
output: html_document
---

```{r SETUP, include=FALSE}
library("mapview")
library("sp")

# load example data
data(meuse)
coordinates(meuse) <- ~x+y
proj4string(meuse) <- CRS("+init=epsg:28992")
```

```{r MAPS}
mapView(meuse, zcol="copper")@map # MAP 1
mapview(meuse, zcol="soil")@map # MAP 2
```

Solution

  • Note, we have implemented the answer provided by @timelyportfolio in package mapview so that this is now easily achievable using mapview::sync(). See ?mapview::sync for instructions and examples.