1

I have been using R-shiny for while, and I wanted to add an image to my leaflet pop-up content. I get a broken image. Although I saved it in a local folder (www) and I called it from there, but its still broken as if it doesn't recognize its an image.

Here is a minimum reproducible example :

library(shiny)
library(leaflet)


city <- paste(sep = "<br/>",
             paste0("<img src='www/image.jpg',width = 50,
       height = 100, ' />")



ui <- fluidPage(
  leafletOutput("map", height = '1000px'))

server <- function(input, output, session) {
  
  output$map <- renderLeaflet({
    leaflet()%>%addTiles() %>%
      #leaflet::addPopups(-122.327298, 47.597131)%>%
      addMarkers(-122.327298, 47.597131, popup  = "city")%>%
    addMarkers(
      lng = -118.456554, lat = 34.105,
      label = "Default Label",
      popup =city,
      labelOptions = labelOptions(noHide = T))

  })
}

shinyApp(ui, server)
rains
  • 23
  • 6
  • 1
    Remove `www/` from the image path. Shiny is looking in there by default, so your path is actually `./www/www/image.jpg`, which is wrong. Hope it helps. – Tom Apr 14 '22 at 09:36
  • @Tom unfortunately it is still broken after removing the www/ . The only time the image appeared was when I used an image on the web address. But I tried using Github folder images link and wasn't lucky either. – rains Apr 19 '22 at 01:00

1 Answers1

0

As mentioned in the comments, images placed under www should be referenced without the www folder name.

Thus, this does the trick for me (N.B. I replaced your paste'd HTML string by using tags from htmltools but this more syntactic sugar):

Code

library(shiny)
library(leaflet)

city <- tags$img(src = "image.jpg", width = 50, height = 100) %>% 
   tagList(tags$br()) %>% 
   as.character() 

ui <- fluidPage(
   leafletOutput("map", height = "1000px")
)

server <- function(input, output, session) {
   
   output$map <- renderLeaflet({
      leaflet() %>% 
         addTiles() %>%
         addMarkers(
            lng = -118.456554, lat = 34.105,
            label = "Default Label",
            popup = city)
      
   })
}

shinyApp(ui, server)

File Structure

Root Folder

Root Folder

www Folder

www folder

Result

Picture in Leaflet Popup

Final Remark

I observed that in my first try, the marker icon was not properly displayed and was indeed a broken image. This was resolved by updating the leaflet library.

thothal
  • 16,690
  • 3
  • 36
  • 71
  • Thank you thothal. Unfortunately neither the code, nor updating leaflet to the latest version helped. I still get a broken image. Why would a web image work and not a locally saved on? When I provide a link to a web image it works find. But when I use my local folder which should be the case it breaks. Could you give a bit of explanation for that? – rains Apr 21 '22 at 01:35
  • The only thing I could imagine are caching issues. Try to [hard refresh](https://fabricdigital.co.nz/blog/how-to-hard-refresh-your-browser-and-clear-cache) – thothal Apr 21 '22 at 08:12
  • Unfortunately its still broken with the hard refresh :( – rains Apr 24 '22 at 10:53
  • It seems to be Chrome related security problem. I inspected the image and it says the items in not found. There is a couple of posts discussing this and I tried downgrading chrome, installing a plugin, forcing Chrome to open the files from my local folders with no luck.. I have the same problem as the user in post https://stackoverflow.com/questions/63622361/favicon-in-shiny-working-with-url-but-not-local-filepath – rains May 05 '22 at 00:42