r-spatial / leafpop

Include Tables, Images and Graphs in Leaflet Popups
Other
113 stars 15 forks source link

ggplot's popup bug #12

Closed lvallata closed 3 years ago

lvallata commented 4 years ago

Here is the link to stackoverflow post (reproducible example inside). https://stackoverflow.com/questions/60741317/shiny-leafpopaddpopupgraphs-bug-looking-for-a-walkaround

Best, Lukic

tim-salabim commented 4 years ago

Sorry for the long silence.

Seems like you need to update the group argument when input$potatoes changes: This works for me:

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 = paste0('A', input$potatoes),
                         label = my.var
        ) %>%
        addPopupGraphs(list(my.plot,my.plot),
                       group = paste0('A', input$potatoes),
                       width = 500, height = 300)

      return(m)
    }

    my_map(input$potatoes)
  })
}

shinyApp(ui, server)

Screenshot from 2020-07-01 19-23-59