如何在 R 中的传单地图中为一条线上的通量设置动画?

How to animate a flux over a line in a leaflet map in R?

如何在 R 中的传单地图中为一条线上的通量设置动画?

有没有办法在 R 中使用 leaflet-ant-path (https://github.com/rubenspgcavalcante/leaflet-ant-path)?

library(sf)
library(leaflet)

pts <-  matrix(1:10, , 2)
ls1 <-  st_linestring(pts)
leaflet() %>% 
   addTiles() %>% 
   addPolylines(data= ls1)

我不确定您的代码显示的是什么,但我可以通过将多个线程捆绑在一起来解决这个问题。首先,我用这个 gist from Joe Cheng to ensure that the antPath library was properly loaded into the browser. To call antPath I needed a CDN and fortunately, there is a CDN which was described in this closed PR

最后,我不得不深入研究 htmlwidgets 中的 onRender 函数以正确执行 js 代码。我在 JS 代码中手动将这些点添加为 LatLong 对象。我确信有一种方法可以直接从 R 会话传递它们,但这就是我用来完成这项工作的方法。

完整代码:

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

antPlugin <- htmlDependency(name = "leaflet-ant-path", version = "1.2.1",
                             src = c(href = "http://unpkg.com/leaflet-ant-path@1.2.1/dist/"),
                             script = "leaflet-ant-path.js"
)

registerPlugin <- function(map, plugin) {
  map$dependencies <- c(map$dependencies, list(plugin))
  map
}

leaflet() %>%
  setView(-0.18, 51.50, 14) %>%
  addTiles() %>%
  registerPlugin(antPlugin) %>%
  onRender("function(el, x) {
    polylinePoints = [
            new L.LatLng(51.51032167, -0.187084072),
            new L.LatLng(51.51019814, -0.187030437),
            new L.LatLng(51.51013137, -0.187845822),
            new L.LatLng(51.50457546, -0.185415744),
            new L.LatLng(51.50476244, -0.181875224),
            new L.LatLng(51.50457546, -0.179622177),
            new L.LatLng(51.50409462, -0.175459380),
            new L.LatLng(51.50368057, -0.174365042),
            new L.LatLng(51.50299938, -0.174729820),
            new L.LatLng(51.50213117, -0.174686903),
            new L.LatLng(51.50199760, -0.177412030),
            new L.LatLng(51.50179725, -0.180373197),
            new L.LatLng(51.50143660, -0.180351735),
         ];
    polylineOptions = {color: 'blue', weight: 6, opacity: 0.9};
    L.polyline.antPath(polylinePoints, polylineOptions).addTo(this);
  }")

这不会显示在 RStudio 的小预览中 window 但扩展到完整的浏览器会显示: