When making maps in R using Leaflet
I often use images in the marker popups by putting an html line. I would like to make a leaflet map in Shiny that allows the user to select photos to go into the marker popups. When I do this from Shiny, the container shows up without an image, and it doesn't allow me to click the image to go to its location on my machine, like a standalone leaflet map would. When I hover over the container, it does display the file name with file:///
out in front of it, just like it does when I output a leaflet map with htmlwidgets
.
Below is a simple working example of the problem. You will just need a .jpg, png, or svg to upload.
ui<-bootstrapPage(div(class="outer",
tags$style(type ="text/css", ".outer {position: fixed; top: 41px; left: 0; right: 0; bottom: 0; overflow: hidden; padding: 0} #table{white-space: nowrap;}"),
leafletOutput("map", width = "100%", height="100%"),
absolutePanel(top = 10, right = 10, width=300, draggable=TRUE,style="background-color: rgba(217, 240, 209, 0.85); border-radius: 10px; padding: 10px",
fileInput(inputId = "photos", label = "Include photos", multiple = T, accept = c('image/png', 'image/jpeg', 'image/svg'))
)))
server<-function(input, output, session) {
photos<- reactive({
if (is.null(input$photos))
return(NULL)
infilee<-input$photos
dirr<-dirname(infilee[1,4])
#reassign that directory to all of the filenames
for ( i in 1:nrow(infilee)) {
file.rename(infilee[i,4], paste0(dirr,"/",infilee[i,1]))}
photo<-list.files(dirr, full.names=TRUE)
})
output$map <- renderLeaflet({
leaflet() %>% addProviderTiles("Esri.WorldImagery") %>%
fitBounds(-81, 34, -77, 40) %>%
addMeasure(
position = "topleft", primaryLengthUnit = "meters", primaryAreaUnit = "acres", secondaryAreaUnit = "sqmeters",
activeColor = "#ff6f69", completedColor = "#00a9ff")
})
observe({
if (is.null(input$photos))
return(NULL)
photos()->phdata
popup<-paste0("<div><a target='_blank' href='",phdata,"'><img width=100%, height=100% src='", phdata,"' ></a></div>")
leafletProxy("map") %>%
addMarkers( lng=-81, lat=37,popup=popup)
})
}
shinyApp(ui = ui, server = server)