Add barplot to custom popup in Leaflet/Shiny

752 views
Skip to first unread message

Nandgate

unread,
Dec 3, 2015, 8:41:45 PM12/3/15
to Shiny - Web Framework for R
Hi,

I am building a shiny app and have used leaflet package to create maps. I am trying to create a custom popup which displays a barchart when it is clicked. Any ideas how to proceed?

Thanks in advance!

Nandgate

unread,
Dec 4, 2015, 6:31:28 PM12/4/15
to Shiny - Web Framework for R
Any suggestions? Waiting for a response!

Thanks

Chris Holcomb

unread,
Dec 10, 2015, 6:17:31 PM12/10/15
to Shiny - Web Framework for R
I think you can pretty much put anything in the pop-up, e.g.:

proxy %>% addPopups(lat=32.530605,lng=-86.7521667,popup=as.character(div(renderPlot(...)))) 

Nandgate

unread,
Dec 10, 2015, 7:48:19 PM12/10/15
to Shiny - Web Framework for R
Hi Chris,

Thanks for your reply! I am using the following sample code. I added your suggestion to it but somehow the plot is still not getting displayed in the pop-up. Can you please take a look and suggest where I am going wrong? I am quite new to Shiny and I am sorry if my question seems very basic!

--SAMPLE CODE--

d <- ggplot(mtcars, aes(factor(cyl))) + geom_bar(fill="white", colour="darkgreen") + geom_bar(width=.5) 

ui <- fluidPage(
  titlePanel("US Map"),
  sidebarPanel( p("Maps")),
  mainPanel(
    h1("Map"),
    leafletOutput("mymap")#, 
    # plotOutput("myplot")
  )
)

server <- function(input, output, session){
  output$mymap <- renderLeaflet({
    c <- paste(sep = "<br/>", "<b>HELLO</b>", "<i>world</i>", 
               as.character(div(renderPlot({d}))), 
               tags$ul(
                 tags$li("First list item"), 
                 tags$li("Second list item"), 
                 tags$li("Third list item")
               ), 
               tags$div(class = "graph", 
                        tags$div(style="height: 22px;", class="bar"), 
                        tags$div(style="height: 6px;", class="bar")),
               tags$img(src = "http://www.rstudio.com/wp-content/uploads/2014/07/RStudio-Logo-Blue-Gradient.png", width = "100px", height = "100px")
    )
    
    leaflet() %>%
      addTiles() %>%
      addMarkers(lng=-86.7521667, lat=32.530605, popup= c)
  })
}
shinyApp(ui, server)

--END OF SAMPLE CODE--

Thanks for your help!

-N

John froeschke

unread,
Dec 11, 2015, 7:10:28 AM12/11/15
to Shiny - Web Framework for R
I don't think you can put reactive type objects in a popup.  Might work if you pre-render the image and call it from the www folder in your app.

hol...@geocf.com

unread,
Dec 18, 2015, 8:49:37 PM12/18/15
to Shiny - Web Framework for R
Whups, my apologies. You can have HTML content in the pop-up, and that can have reactively defined data in it but you can't have graphics in there out of the box. You'll need to use addPopups to add the pop-ups upon click events instead of pre-loading via addMarkers, but the HTML won't change until you remove and add it back or add a different one. You could try what John suggested and reactively pre-render the plot as an image and have the HTML link to that temporarily stored image. You might also be able to generate some HTML for a static ggvis plot.

FWIW, I'll probably spend some more time on this and report back or make a feature request, as this would be useful for us (right now we're generating absolutePanels on map clicks for this reason and because we didn't know how to make events from pop-ups - you can with Shiny.onInputChange).

Chris
Reply all
Reply to author
Forward
0 new messages