Search code examples
rshinyr-leaflet

Shiny leafpop::addPopupGraphs bug, looking for a walkaround


I experienced a strange behavior of the leafpop function addPopupGraphs. This is a reproducible example illustrating the problem.

After the execution, if you click on a marker, a popup-graph appears. If you just move the slide-bar and click again on the marker no popup appears, I want to make it appear.

I saw that if I just comment the row label = my.var, the popups work fine, but I need the dependency from variable my.var.

library(shiny)
library(leaflet)
library(leafpop)
library(ggplot2)

ui = fluidPage(
  sliderInput(inputId = "potatoes",
              label = "Potatoes:",
              min = 1,
              max = 10,
              value = 2,
              step = 1,
              animate = F,
              width = '100%'),
  leafletOutput('my_map', height = 700)
)

server = function(input, output, session) {

  output$my_map <- renderLeaflet({

    my_map <- function(my.var = character()){

      my.plot <- ggplot(mtcars, aes(cyl, mpg)) + geom_line()

      m <- leaflet() %>%
        addTiles(urlTemplate = 'https://{s}.tile.opentopomap.org/{z}/{x}/{y}.png') %>%
        setView(lng = 10,
                lat = 49,
                zoom = 4) %>%
        addCircleMarkers(lng = c(10, 10),
                         lat = c(49, 50),
                         group = 'A',
                         label = my.var
                         ) %>%
        addPopupGraphs(list(my.plot,my.plot),
                       group = 'A',
                       width = 500, height = 300)

      return(m)
    }

    my_map(input$potatoes)
  })
}

shinyApp(ui, server)

Solution

  • See https://github.com/r-spatial/leafpop/issues/12#issuecomment-652549650 for a solution. You need to change the group argument with your input, then it will render properly.