Search code examples
rshinyr-leaflet

Add a logo to a leaflet map in a shiny app


I would like to add a logo to a leaflet map in a shiny app.

The addLogo function of the leafem package allows this, when I generate a map outside the shiny environment the function works perfectly, however, when applying the function in shiny it does not work.

I do not know what I can be obviating or if there is another way to do it.

├── app.R
└── www
    └── Logo.png
library(leaflet)
library(shiny)
library(leafem)


ui <- fluidPage(
  
  leafletOutput("map")
  
)

server <- function(input, output, session) {
  
  output$map <- renderLeaflet({    
    
    leaflet() %>%
      addTiles() %>%
      setView(lng = -79.442471,
              lat = 43.6857,
              zoom = 12) %>%
      addLogo("Logo.png",
              src= "local")    
    
    
  })
  
  
}

shinyApp(ui, server)




Solution

  • Use src= "remote" in addLogo. Even though the Shiny app and image is in your local computer, you need to use it as remote. Using local will point to ../graphs/Logo.png instead of only Logo.png (that is the default for files under the www directory).

    library(leaflet)
    library(shiny)
    library(leafem)
    
    
    ui <- fluidPage(
      
      leafletOutput("map")
      
    )
    
    server <- function(input, output, session) {
      
      output$map <- renderLeaflet({    
        
        leaflet() %>%
          addTiles() %>%
          setView(lng = -79.442471,
                  lat = 43.6857,
                  zoom = 12) %>%
          addLogo("Logo.png",
                  src= "remote")    
      })
      
      
    }
    
    shinyApp(ui, server)