R中具有串扰和传单的响应式热图 [英] responsive heatmap with crosstalk and leaflet in R

查看:40
本文介绍了R中具有串扰和传单的响应式热图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用R中的串扰绘制点和响应热图的地图.像这样:

I want to make a map with points and a responsive heatmap using crosstalk in R. Like this:

library(crosstalk)
library(leaflet)
library(DT)

# Wrap data frame in SharedData
sd <- SharedData$new(quakes[sample(nrow(quakes), 10),])

bscols(
  # Create a filter input
  filter_slider("mag", "Magnitude", sd, column=~mag, step=0.1, width=250),
  leaflet(sd) %>% addTiles() %>% addMarkers() %>% addHeatmap())
)

但是,正如运行代码后可以看到的那样,这种方式在过滤时不会对热图产生响应效果

But, as can be observed after running the code, this way doesn't get the responsive effect on the heatmap when filtering

如何达到效果?

推荐答案

这可以通过 Shiny 轻松完成.但是,如果您真的不想使用串扰,那么每次更改标记时都必须添加一些JavaScript以重绘热图,因为由于某些原因,串扰似乎无法做到这一点.

This could be easily done with Shiny. However, if you really wan't to use crosstalk, then you would have to add some javascript to redraw the heat map every time the markers are changed as crosstalk for some reason can't seem to do this.

可以在此处找到一个工作示例: https://rpubs.com/Jumble/r_crosstalk_leaflet_heatmap_update

A working example can be found here: https://rpubs.com/Jumble/r_crosstalk_leaflet_heatmap_update

下面是产生此代码的代码:

Below is the code which produced this:

library(crosstalk)
library(leaflet)
library(leaflet.extras)
library(dplyr)

# Wrap data frame in SharedData
sd <- SharedData$new(quakes[sample(nrow(quakes), 10),])

bscols(widths=c(3,9),
  # Create a filter input
  filter_slider("mag", "Magnitude", sd, column=~mag, step=0.1),
  leaflet(sd) %>% 
    addTiles() %>% 
    addMarkers() %>% 
    addHeatmap(layerId="heatmap") %>%
    removeHeatmap("heatmap") %>%
    htmlwidgets::onRender("
      function(el,x){
        var myMap = this;
        var coord_state;
        var coords;
        
        function get_markers(){
          coord_state = [];
          myMap.eachLayer(function(layer){
            if (typeof layer.options.lat != 'undefined'){
              coord_state.push([layer.options.lat, layer.options.lng, 0.5]);
            }
          })
          return(coord_state)
        }
        
        function update_layer(){
          coords = get_markers()
          heat1.setLatLngs(coords);
          heat1.redraw();
        }
        
        var heat1 = L.heatLayer(get_markers(), {radius: 25}).addTo(myMap);
        myMap.on('layerremove', update_layer);
        myMap.on('layeradd', update_layer);
      }
    "))

这篇关于R中具有串扰和传单的响应式热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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