Leaflet.LabelTextCollision plugin doesn't work in R with leaflet

Viewed 48

Leaflet.LabelTextCollision plugin is a usefull feature for spatial visualisation. It hides labels on the map, which overlap with each other.

I try to use Leaflet.LabelTextCollision in R with leaflet package, but it doesn't work. What did i miss? I'm not familiar with JS, but, as far as i understand, i should implement JS function in the R code (see onRender in the example).

I asked the same question on Github a year ago, but still I haven't recieved an answer (https://gist.github.com/jcheng5/c084a59717f18e947a17955007dc5f92?permalink_comment_id=3814855#gistcomment-3814855).

Link to the Leaflet.LabelTextCollision plugin: https://github.com/yakitoritabetai/Leaflet.LabelTextCollision

Here is an example of my code.

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

setwd("D:/Data/page_ex")


label <- data.frame(
lat = c(61.09049, 56.89039, 57.52678, 60.74516, 56.92379, 64.54302, 56.25897, 56.49648, 56.27996, 56.74812, 59.93873, 56.77972, 56.85867, 60.08370, 56.25897),
lon = c(43.17148, 32.65250,  38.31669, 42.04732, 32.74461, 40.53712, 32.08586, 31.64108, 31.66774, 33.51162, 30.31623, 31.25263, 35.92083, 30.27957, 32.08586),
name_label = c("label_1", "label_2", "label_1000", "label_10000", "label_70000", "label_8", "label_999999", "label_777", "label_888888", "label_888", "label_999", "label_7", "label_9", "label_777777777", "label_999999999")
)


col_Plugin <- htmlDependency(
  "Leaflet.LabelTextCollision", "1.4.0",
  src = "./Leaflet.LabelTextCollision-master/dist",
  script = "L.LabelTextCollision.js"
)


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



leaflet() %>%
  
  setView(35, 60, zoom = 4) %>%
  
  addProviderTiles(provider =  "OpenStreetMap") %>%
  
  registerPlugin(col_Plugin) %>%

  addLabelOnlyMarkers(lng = label$lon,
                      lat = label$lat,
                      label = lapply(label$name_label, HTML),
                      labelOptions = labelOptions(noHide = T,
                                                  textOnly = F,
                                                  direction = "top")
  ) %>% 
  
  onRender("function(el, x) {
  
  L.LabelTextCollision(collisionFlg : true).addTo(this);}")
0 Answers
Related