I've been trying to create a Leaflet map visual in R that syncs two maps (two maps are shown next to each other and when you move/zoom on one map the other map moves along with that). This works fine using the R leafsync package, see this picture. However, the end goal is to use this R Visual in a PowerBI report and the leafsync package is not supported by PowerBI. Therefore, I would like to use the Javascript Leaflet.Sync package in a OnRender function when creating the R visual. Below is the code I have so far, where map1 and map2 are earlier crated Leaflet map visuals in R:
# A function that takes a plugin htmlDependency object and adds
# it to the map. This ensures that however or whenever the map
# gets rendered, the plugin will be loaded into the browser.
registerPlugin <- function(map, plugin) {
map$dependencies <- c(map$dependencies, list(plugin))
map
}
# This tells htmlwidgets about our plugin name, version, and where to find the script
syncPlugin <- htmlDependency("Leaflet.Sync", "0.2.4",
src = c(href = "https://cdn.jsdelivr.net/gh/jieter/Leaflet.Sync/"),
script = list("L.Map.Sync.js"))
map2 = map2 %>%
# Register Sync Plugin on this map instance
registerPlugin(syncPlugin) %>%
# Add your custom JS logic here. The `this` keyword refers to the Leaflet (JS) map object.
onRender("function(el, x, data) {
this.sync(data);
data.sync(this);
}", data=map1)
map2
This is currently not working (the result I get is the original map2 visual that is unable to move or zoom, the map1 visual is nowhere to be seen), however I'm not sure why and thus I have a couple of questions:
Many thanks in advance for any help provided! :)