在R中使用传单并排插件 [英] using leaflet-side-by-side plugin in R

查看:13
本文介绍了在R中使用传单并排插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 使用任意 Leaflet JS 插件和 Leaflet for R.看起来很简单,到目前为止还没有成功.我无法弄清楚我做错了什么.非常感谢您的回复.谢谢,

I tried to implement leaflet-side-by-side plugin using example codes from Using arbitrary Leaflet JS plugins with Leaflet for R. Appears simple, no success so far. I could not figured out what I'm doing wrong. Greatly, appreciate your reply. Thanks,

library(leaflet)
library(htmltools)
library(htmlwidgets)


LeafletSideBySidePlugin <- htmlDependency("leaflet-side-by-side","2.0.0",
                                          src = c(href="https://github.com/digidem/leaflet-side-by-side"),
                                          script="leaflet-side-by-side.js")

# 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
}

leaflet() %>% addTiles() %>%
   setView(lng = 12, lat = 50, zoom = 4) %>%
   # Register leaflet-side-by-side plugin on this map instance
   registerPlugin(LeafletSideBySidePlugin) %>%
   onRender("
            function(el, x) {
var mylayer1 = L.tileLayer(
          'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',{
            maxZoom: 18
            })
var mylayer2 = L.tileLayer(
          '//stamen-tiles-{s}.a.ssl.fastly.net/watercolor/{z}/{x}/{y}.png',{
            maxZoom: 14
            })
            L.control.sideBySide(mylayer1, mylayer2).addTo(this);
            ")

推荐答案

我要赞扬 Abuw 的回答.如果有人想知道为什么它仍然不起作用,那是因为 onRender JavaScript 中有一个不匹配的{".

I would like to commend Abuw for his answer. If anyone is wondering why it still doesn't work, it's because there is an unmatched '{' in the onRender JavaScript.

还要确保您的 htmlDependency 源确实存在.jsdelivr 提供了大多数通过 npm 和 github 以 application/javascript 格式提供的 js 库.使用它而不是原始 github 路径,完整的工作代码应如下所示:

Also make sure that your htmlDependency source actually exists. jsdelivr provides most js libraries available through npm and github in application/javascript format. Using that instead of the raw github path you, the full working code should be as follows:

library(leaflet)
library(htmltools)
library(htmlwidgets)


LeafletSideBySidePlugin <- htmlDependency("leaflet-side-by-side","2.0.0",
                                          src = c(href="https://cdn.jsdelivr.net/gh/digidem/leaflet-side-by-side@2.0.0/"),
                                          script="leaflet-side-by-side.js")

# 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
}

leaflet() %>% addTiles() %>%
  setView(lng = 12, lat = 50, zoom = 4) %>%
  # Register leaflet-side-by-side plugin on this map instance
  registerPlugin(LeafletSideBySidePlugin) %>%
  onRender("
        function(el, x) {
          var mylayer1 = L.tileLayer(
            'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',{
             maxZoom: 18
             }).addTo(this);
          var mylayer2 = L.tileLayer(
          '//stamen-tiles-{s}.a.ssl.fastly.net/watercolor/{z}/{x}/{y}.png',{
             maxZoom: 14
             }).addTo(this);
        L.control.sideBySide(mylayer1, mylayer2).addTo(this);
        }")

这篇关于在R中使用传单并排插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆