tomroh / leaflegend

Provides extensions to the leaflet R package to customize legends with images, text styling, orientation, sizing, and symbology.
https://leaflegend.delveds.com
Other
34 stars 3 forks source link

icons not rendering in addLegendAwesomeIcon() #50

Closed Ben-Cox closed 2 years ago

Ben-Cox commented 2 years ago

I can't seem to get leaflegend to display icons used in AwesomeMarkers. There are open issues with certain Font Awesome icons and I've implemented the workaround at https://github.com/rstudio/leaflet/issues/691 for getting the icon to show on the map but I can't figure out how to make them show up in addLegendAwesomeMarkers(). I'm primarily an R user and know next to nothing about HTML/CSS. Is there something I can pass to addControl() in addLegendAwesomeMarkers(...) that can use the text representation of the icon?

Thanks!

Reprex:

library(shiny)
library(leaflet)
library(leaflegend)
library(fontawesome)

# dummy data
set.seed(123)
d <- tibble(lat=rnorm(2), lng=rnorm(2), col=c('gray','red')) 

iconList1 <- awesomeIconList(
  gray=makeAwesomeIcon(icon='fish', library='fa', markerColor = 'lightgray', iconColor = 'black'),
  red=makeAwesomeIcon(icon='fish', library='fa', markerColor='red', iconColor='black')
)

# no fish icon
d %>% 
  leaflet() %>% 
  addTiles() %>% 
  addAwesomeMarkers(icon=~iconList1[col]) %>% 
  addLegendAwesomeIcon(iconSet=iconList1)

# Shiny finds the icon in fa library
shiny::icon("fish",lib='font-awesome')

# Proposed solution at: https://github.com/rstudio/leaflet/issues/691 using text argument with fontawesome.
iconList2 <- awesomeIconList(
  gray=makeAwesomeIcon(icon='fish', library='fa', text=fontawesome::fa("fish"), markerColor = 'lightgray', iconColor = 'black'),
  red=makeAwesomeIcon(icon='fish', library='fa', text=fontawesome::fa("fish"), markerColor='red', iconColor='black')
)

# icon shows in marker but not legend
d %>% 
  leaflet() %>% 
  addTiles() %>% 
  addAwesomeMarkers(icon=~iconList2[col]) %>% 
  addLegendAwesomeIcon(iconSet=iconList2)
tomroh commented 2 years ago

The text parameter is now supported and will be included in the next release. The "iconList2" example will work as expected. Install the devel branch from github (remotes::install_github('tomroh/leaflegend@devel'))

For reference: leaflet currently only supports font awesome icons from version 4 so if an icon from a later version is not in 4 then no icon will appear. The above workaround converts the font icon to an svg so that it can be rendered regardless of the font awesome icon library that is included as a source for a leaflet map.

1c42f459b255f9683be603361f5ce3ee3844cb44