在我的R代码中添加一个传单插件是有困难的。具体来说,我正在尝试添加Leaflet.zoomhome插件(参见这个演示中左上角)。
我一直试图跟踪本指南,但没有成功,这就是我到目前为止所拥有的,它没有起作用。
library(leaflet)
library(htmltools)
library(htmlwidgets)
library(dplyr)
map <-
leaflet::leaflet() %>%
leaflet::addTiles(group = "OSM")
zoom_home_plugin <- htmlDependency(
name = "leaflet.zoomhome",
version = "99.99.99",
src = c(href = "https://github.com/torfsen/leaflet.zoomhome/tree/master/dist/"),
script = "leaflet.zoomhome.js")
RegisterPlugin <- function(map, plugin) {
map$dependencies <- c(map$dependencies, list(plugin))
return(map)
}
map <- RegisterPlugin(map, zoom_home_plugin) %>%
onRender("function(el, x) {
zoomHome.addTo(this);
}")
map对于导致错误的原因以及如何重写代码以实现插件,有什么想法吗?
提前谢谢你。
发布于 2019-12-10 21:25:00
我有个密码:
library(leaflet)
library(tidyverse)
library(htmlwidgets)
library(htmltools)
map <- leaflet(options = leafletOptions(zoomControl = FALSE)) %>%
addTiles() %>%
onRender(
"
function(el,x) {
var zoomHome = L.Control.zoomHome();
zoomHome.addTo(this);
}
")
temp_folder <- tempdir()
download.file(
"https://raw.githubusercontent.com/torfsen/leaflet.zoomhome/master/dist/leaflet.zoomhome.js",
file.path(temp_folder, 'leaflet.zoomhome.js')
)
download.file(
"https://raw.githubusercontent.com/torfsen/leaflet.zoomhome/master/dist/leaflet.zoomhome.css",
file.path(temp_folder, 'leaflet.zoomhome.css')
)
tagList(
tags$head(
includeScript(file.path(temp_folder, 'leaflet.zoomhome.js')),
includeCSS(file.path(temp_folder, 'leaflet.zoomhome.css')),
tags$link(rel="stylesheet", href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css"),
),
map
) %>%
browsable()据我所知,您的代码试图访问"https://github.com/torfsen/leaflet.zoomhome/tree/master/dist/“,但正确的方法是指向"https://raw.githubusercontent.com/torfsen/leaflet.zoomhome/master/dist/leaflet.zoomhome.js”。即使这样,在头中放置一个<script>标记似乎并不适用于原始github链接,因为js和样式表没有加载(至少在我所有的尝试中都是如此)。因此,作为解决办法,我将脚本和样式表直接下载到临时文件夹中,并在html头中使用该文件。
此外,我还添加了fontawesome依赖项,并修改了onRender脚本以匹配leaflet.zoomhome自述中的示例。
https://stackoverflow.com/questions/59250140
复制相似问题