r-spatial/leafem

Logo disappear after a second click on a button

elgabbas opened this issue · 6 comments

Hello,

I noticed that by using the addLogo function within a shiny app, the logo appears after the first button click and disappear in subsequent clicks. Please find the reprex below:

library(shiny)
library(leaflet)

r_colors <- rgb(t(col2rgb(colors()) / 255))
names(r_colors) <- colors()

ui <- fluidPage(
  leafletOutput("mymap"),
  p(),
  actionButton("recalc", "New points")
)

server <- function(input, output, session) {
  
  points <- eventReactive(input$recalc, {
    cbind(rnorm(40) * 2 + 13, rnorm(40) + 48)
  }, ignoreNULL = FALSE)
  
  output$mymap <- renderLeaflet({
    leaflet() %>%
      addProviderTiles(providers$Stamen.TonerLite,
                       options = providerTileOptions(noWrap = TRUE)
      ) %>%
      addMarkers(data = points()) %>% 
      leafem::addLogo(
        img = "https://upload.wikimedia.org/wikipedia/commons/f/f7/AWI_Logo_2017.svg",
        url = "http://www.awi.de/",
        position = "bottomleft",
        alpha = 0.3, width = 120, height = 120,
        # offset.x = 30, offset.y = 30
      )
  })
}

shinyApp(ui, server)

Is it a shiny-specific question, or the addLogo function may need to consider this in the future?

Cheers,
Ahmed

Hello,

I hope this reprex helps to illustrate the issue:

library(shiny)
library(leaflet)

r_colors <- rgb(t(col2rgb(colors()) / 255))
names(r_colors) <- colors()

ui <- fluidPage(
  leafletOutput("mymap"),
  p(),
  actionButton("recalc", "New points")
)

server <- function(input, output, session) {
  
  points <- eventReactive(input$recalc, {
    cbind(rnorm(40) * 2 + 13, rnorm(40) + 48)
  }, ignoreNULL = FALSE)
  
  output$mymap <- renderLeaflet({
    leaflet() %>%
      addProviderTiles(providers$Stamen.TonerLite,
                       options = providerTileOptions(noWrap = TRUE)
      ) %>%
      addMarkers(data = points()) %>% 
      leafem::addLogo(
        img = "https://upload.wikimedia.org/wikipedia/commons/f/f7/AWI_Logo_2017.svg",
        url = "http://www.awi.de/",
        position = "bottomleft",
        alpha = 0.3, width = 120, height = 120,
        # offset.x = 30, offset.y = 30
      )
  })
}

shinyApp(ui, server)

Cheers,
Ahmed

I have another question regarding the addLogo function. Is it possible to open the link provided to the addLogo function in a new tab? This should be easy but I could not find a solution for it. Is it possible in future releases to add an argument for that?

@elgabbas this likely needs a complete refactoring of the addLogo function, so don't expect it to be resolved too soon. See also https://github.com/mtennekes/tmap/issues/461 which is basically the same issue.

Thanks for your response. At least I know this has nothing to do with a potential bug in my shiny application.

You could rearrange your code a bit and use leafletProxy to add/change the points.

library(shiny)
library(leaflet)

r_colors <- rgb(t(col2rgb(colors()) / 255))
names(r_colors) <- colors()

ui <- fluidPage(
  leafletOutput("mymap"),p(),
  actionButton("recalc", "New points")
)

server <- function(input, output, session) {
  points <- eventReactive(input$recalc, {
    cbind(rnorm(40) * 2 + 13, rnorm(40) + 48)
  }, ignoreNULL = FALSE)
  
  output$mymap <- renderLeaflet({
    leaflet() %>%
      addProviderTiles(providers$Stamen.TonerLite,
                       options = providerTileOptions(noWrap = TRUE)) %>% 
      leafem::addLogo(
        img = "https://upload.wikimedia.org/wikipedia/commons/f/f7/AWI_Logo_2017.svg",
        url = "http://www.awi.de/",
        position = "bottomleft",
        alpha = 0.3, width = 120, height = 120) %>% 
      setView(9, 49, 5)
  })
  
  observe({
    leafletProxy("mymap") %>%
      clearGroup("pts") %>% 
      addMarkers(data = points(), group = "pts")
  })
}

shinyApp(ui, server)

Thanks @trafficonese. This works for me.