使用 "R for leaflet" 编辑地图

Edit map with "R for leaflet"

我有一个脚本可以让我生成地图 "R for leaflet" :

library(htmlwidgets)
library(raster)
library(leaflet)

# PATHS TO INPUT / OUTPUT FILES
projectPath = "path"
#imgPath = paste(projectPath,"data/cea.tif", sep = "")
#imgPath = paste(projectPath,"data/o41078a1.tif", sep = "") # bigger than standard max size (15431804 bytes is greater than maximum 4194304 bytes)
imgPath = paste(projectPath,"/test.tif", sep = "")
outPath = paste(projectPath, "/leaflethtmlgen.html", sep="")

# load raster image file
r <- raster(imgPath)

# reproject the image, if necessary
#crs(r) <- sp::CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")

# color palette, which is interpolated ?
pal <- colorNumeric(c("#FF0000", "#666666", "#FFFFFF"), values(r),
                    na.color = "transparent")

# create the leaflet widget
m <- leaflet() %>%
  addTiles() %>%
  addRasterImage(r, colors=pal, opacity = 0.9, maxBytes = 123123123) %>%
  addLegend(pal = pal, values = values(r), title = "Test")


# save the generated widget to html
# contains the leaflet widget AND the image.
 saveWidget(m, file = outPath, selfcontained = FALSE, libdir = 'leafletwidget_libs')

我的问题是这是生成一个 html 文件,我需要这张地图是动态的。例如,当用户点击一些未集成在地图上的 html 按钮时,我想在地图上添加一个矩形。欢迎任何解决方案...

Leaflet 本身不提供您正在寻找的交互功能。一种解决方案是使用 shiny, which is a web application framework for R. From simple R code, it generates a web page, and runs R on the server-side to respond to user interaction. It is well documented, has a gallery of examples, and a tutorial 让新用户入门。

它适用于 leaflet. One of the examples on the shiny web site uses it, and also includes a link to the source code

更新

实际上,如果简单的 showing/hiding 个元素就足够了,使用 groups 单独的传单就足够了。从这个问题来看,你需要它有多动态还不是很清楚。