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