Luka
Luka

Reputation: 43

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)

Upvotes: 0

Views: 470

Answers (3)

Mehrdad Varedi
Mehrdad Varedi

Reputation: 11

Thanks for the solution @AF7. I tried both solutions from @TimSalabim and you. Both works, although yours was better because of less complication. I found that the mapview::popupGraph doesn't exist anymore in mapview package (It is Dec31,2022). It only exists in leafpop::popupGraph.

Inspired from your code to use mapview and a page here: https://environmentalinformatics-marburg.github.io/mapview/popups/html/popups.html

I wrote the popup with mapview instead of leaflet:

mapview(myMapObject, popup = leafpop::popupGraph(Graphs,width=200,height=200))@map

which worked in shiny with no problem.

Graphs were the list of Graphs which the number of those should match the number (and order) of the myMapObject. The myMapObject can be a point layer or a polygon.

my case, I had a polygon object and I had calculated the graphs for it,

Upvotes: 0

TimSalabim
TimSalabim

Reputation: 5844

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.

Upvotes: 1

AF7
AF7

Reputation: 3242

As a workaround, you could use mapview::popupGraph() instead. It works for me! More info here.

I just passed popup = mapview::popupGraph(...) to your marker function. Something like this:

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,
      ## Replaced leafpop::addPopuppGraph() here
                         popup = mapview::popupGraph(list(my.plot,my.plot))
                         )

      return(m)
    }

    my_map(input$potatoes)
  })
}

shinyApp(ui, server)

I'm sure the guys at leafpop would love a bug report ;).

Upvotes: 2

Related Questions