Search code examples
rshinyr-leaflet

Custom icons doesn't render


I'm struggling with custom icons for my Shiny app on mobile devices (on desktop it works properly). I have read this topic but it didn't help me to manage seeing proper icons: R Shiny: publishing leaflet maps with custom icons

If I use the default icon everything works fine, it prints properly. I keep my icons in www folder inside the app directory. Does it mean that custom icons don't work?

If I use my icons either as a link or an image inside the app directory it doesn't work on mobile (on desktop it does).

The code responsible for printing is as follow (these are just chunks of the code, it is not reproducible, but those parts should be enough to tell if I'm doing something wrong)

ui <- dashboardPage(
  dashboardHeader(title  = "Location Tracker", titleWidth = "270px"),
  dashboardSidebar(width = "270px"),
  dashboardBody(leafletOutput("mymap", width = "auto", height = "560px"))
)


server <- shinyServer(function(input, output, session) {

# some other code

output$mymap <- renderLeaflet({
      leaflet <- leaflet() %>%
      addTiles() %>%
    return(leaflet)
  })


iconize <- function(icon_file_name, x_size, y_size){
      icon <- iconList(
      ship = makeIcon(icon_file_name, x_size, y_size)
    )
  }

  home_icon <- iconize("https://lh5.googleusercontent.com/IoAku0AlWU1-9KBAccr8n0QI01K8egDndIKbEZSIh2uUIjQd_WUPKUCQMUeorSAW_sPB4BRHKuiTmm8vub68=w1410-h916", 25, 25) 
  tram_icon <- iconize("www/tram_icon.png", 35, 35)
  bus_icon <- iconize("www/bus_icon.png", 35, 35)

observeEvent(autoInvalidate(), {
    leafletProxy("mymap") %>%
      clearMarkers() %>%
      addMarkers(
        data = tram_points(),
        label = tram_labels(),
        icon = tram_icon
        )
  },ignoreNULL = FALSE)
})

Solution

  • Finally after few days I have come up with a solution. Namely, I used Font Awesome Icons and everything works perfectly.

    Some code below:

    icon.home <- makeAwesomeIcon(icon = 'home', library = "fa", markerColor = "green")
    
    observeEvent(autoInvalidate(), {
        leafletProxy("mymap") %>%
          clearMarkers() %>%
          addAwesomeMarkers(
            lng = tram_points()$Lon,
            lat = tram_points()$Lat,
            icon = icon.home
          )})