John Smith
John Smith

Reputation: 2886

R using Heatmaps in Leaflet

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?

Upvotes: 3

Views: 3840

Answers (1)

John Smith
John Smith

Reputation: 2886

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))

Upvotes: 3

Related Questions