Display plot in leaflet popup.

1,040 views
Skip to first unread message

Dalton Hance

unread,
Jun 26, 2015, 12:25:06 PM6/26/15
to shiny-...@googlegroups.com
I'm attempting to display plot output in a leaflet pop-up when a user clicks on a polygon. The specific application is that I've got a hexagonal grid covering an area and I'm simulating a spatial point process within those polygons. So each polygon has some points associated with this that are stored in a list. My solution has been to save a temporary .png file whenever the user clicks on the polygon and then display that image using HTML tags.

Here is the observer  and showPopup functions:
observe({
    leafletProxy("map") %>% clearPopups()

    event <- input$map_shape_click
 
    if (is.null(event))
      return()
    
    if ( file.exists("www/temp.png") ) {
      file.remove("www/temp.png")
      print("temp.png removed")
    }
    
    png("www/temp.png")
    plot(mussels[[as.integer(event$id)]], main="")
    dev.off()
    
    isolate({
      showPopup(event$id, event$lat, event$lng)
    })
  })

  showPopup <- function (id, lat, lng) {
    selectedhex <- hab.grid@data[hab.grid@data$OBJECTID == id,]
    content <- as.character(tagList(
      tags$h4(paste("Number of Mussels:", selectedhex$NumberofMussels)),
      tags$h4(paste("River Zone:", selectedhex$RiverZone)),
      tags$img(src="temp.png", width = 300)
      ))
    leafletProxy("map") %>% addPopups(lng, lat, content)
  }

This works fine when run in RStudio, i.e. whenever I click on a polygon a new plot is displayed, but when I open in browser (Chrome) I'm running into an issue where the browser caches the first plot generated and so will continue to display that image every time the use clicks on a new polygon. Any ideas on a workaround for this. I suppose I could save every potential png file with a unique filename and that should solve it, but it seem inelegant and inefficient. Ideally, I'd be able to do this dynamically without saving an image at all, perhaps by using the plotOutput/renderPlot functions. The problem with this approach is that,as far as I know, the content for the addPopups function needs to be defined in the server.R, not ui.R

Dalton Hance

unread,
Jun 26, 2015, 1:24:15 PM6/26/15
to shiny-...@googlegroups.com
I guess I'll answer my own question. The solution I hit upon (maybe not the best one, and so I'm still open to suggestions) is to force a new URL for the image each time the user clicks on the polygon. I accomplished this by changing the following line in the showPopup function:
      tags$img(src=paste("temp.png?",as.numeric(Sys.time()), sep=""), width = 300)


Works like a charm.

Yihui Xie

unread,
Jun 26, 2015, 9:18:50 PM6/26/15
to Dalton Hance, shiny-discuss
That is a smart hack :) I agree it will be nice to be able to use
renderXXX()/XXXOutput() functions for the popups. A similar feature
request has been filed here:
https://github.com/rstudio/leaflet/issues/120

Regards,
Yihui

Joe Cheng

unread,
Jun 29, 2015, 6:46:54 PM6/29/15
to Yihui Xie, Dalton Hance, shiny-discuss
You could do what you're doing but instead of using www/temp.png, encode the image as a data: URI and assign it as the img src. There's a version of this in the leaflet::addRasterImage function.
--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/CANROs4dph4ZNi24R3_sfsv%2BMH6%3D7Z-FO9fq7J%2BpxMmunfi0usA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages