3

I have a Linux box that runs Shiny I'm trying to get the code to run for leaflet based on the demos here and here which look brilliant

My code is below which is taken from the rpubs page

library(leaflet)
library(leaflet.extras)
leaflet(quakes) %>% addProviderTiles(providers$CartoDB.DarkMatter) %>%
  addWebGLHeatmap(lng=~long, lat=~lat, intensity = ~mag, size=60000)

I have installed /home/shiny/nodejs/Leaflet.heat-gh-pages

When I run the code above I get the map. My data is good because I can plot the markers, but nothing seems to happen when I add the addWebGLHeatmap portion.

I am a complete novice at JS but is there any additional setup I need to get it running?

MLavoie
  • 9,671
  • 41
  • 36
  • 56
John Smith
  • 2,448
  • 7
  • 54
  • 78
  • 1
    What do you mean by "nothing happens"? If there's just a blank map it could mean there's problem with your browser is too old or doesn't support WebGL. You can test it here, for example: https://get.webgl.org/ – chrki Apr 19 '17 at 19:09
  • Hi @chrki, The map loads but the points (the heat mapping aspect of my data) doesnt overlay the OSM map – John Smith Apr 20 '17 at 08:12

1 Answers1

3

It seems i have to register the plugin first in order for it to work as per the github page here

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

heatPlugin <- htmlDependency("Leaflet.heat", "99.99.99",
  src = c(href = "http://leaflet.github.io/Leaflet.heat/dist/"),
  script = "leaflet-heat.js"
)

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

leaflet() %>% addTiles() %>%
  fitBounds(min(quakes$long), min(quakes$lat), max(quakes$long),     max(quakes$lat)) %>%
  registerPlugin(heatPlugin) %>%
  onRender("function(el, x, data) {
    data = HTMLWidgets.dataframeToD3(data);
    data = data.map(function(val) { return [val.lat, val.long, val.mag*100]; });
L.heatLayer(data, {radius: 25}).addTo(this);
  }", data = quakes %>% select(lat, long, mag))
John Smith
  • 2,448
  • 7
  • 54
  • 78