In Shiny R, I'm making an app of a map with overlays, which then adds an overlay of input from the user. So when after the user uploads his data and sees the updated map with the markers, I'd like for the user to be able to download the new map as an html file.
I'll just make a simplified demo of what I'm doing below:
server.ui
library(shiny)
library(leaflet)
shinyServer(function(input,output,session){
output$mymap <- renderLeaflet({
mydata <- read.csv("data/stuff.csv")
leaflet() %>% addTiles() %>%
addCircles(data = mydata)
})
observeEvent(input$file1, {
inUserfile <- input$file1
if(!is.null(inUserfile)) {
userdata <- read.csv(inUserfile$datapath)
proxy <- leafletProxy("mymap")
proxy %>% addMarkers(data = userdata)
}
})
output$htmllink <- downloadHandler (
filename = 'temp.html',
content = function(file) {
src <- normalizePath('mymap.Rmd')
owd <- setwd(tempdir())
on.exit(setwd(owd))
file.copy(src, 'mymap.Rmd')
out <- render('mymap.Rmd', html_document())
file.rename(out,file)
})
I can't seem to get the download button to work though. I'm not sure what to put in the .Rmd file, especially because I'm also using a user-uploaded file.
Documentation is pretty fuzzy when it comes to saving the maps after using leafletProxy, what I'm trying to adapt is from questions by other people trying to save their leaflet outputs in Shiny R.
Any help is appreciated.