I have a module with only the download button and 2 other modules, the ui and server functions to plot a map.
Now I want to implement the download button to print the map. I have tried many ways but I can't download the map in a pdf.
In my original script, I have several modules to plot several charts. I will have to be able to download all the charts in one pdf in portrait.
The download is not working. I have tried this example: https://community.rstudio.com/t/shiny-module-downloading-all-plots-into-a-single-pdf/124869
How can I download the map in a pdf?
Here is my reproducible code:
#The map UI
trade_agreement_ui <- function(id) {
ns <- NS(id)
tagList(
fluidRow(column(
8,
offset = 2,
box(
solidHeader = TRUE,
column(12, align = 'left', h4(strong("Trade Agreement"))),
br(),
column(
12,
align = 'left',
br(),
leafletOutput(outputId = ns("map1"),height = "650px", width = "100%")
),
width = 16
)
)))
}
#The map server
trade_agreement_server <- function(id) {
moduleServer(id,
function(input, output, session) {
val <- reactiveValues(map1=NULL)
output$map1 <- renderLeaflet({
val$map1 <- leaflet() %>%
addTiles(urlTemplate = "https://api.mapbox.com/styles/v1/bholee/cl75rvfqs002q14o0rwzd6oe5/tiles/{z}/{x}/{y}@2x?access_token=pk.eyJ1IjoiYmhvbGVlIiwiYSI6ImNrN2tibG9pNzAwajMzbWw4ZnlpcDNqY2wifQ.o-qJAmRdkh-McoubI4E2DA"
)
val$map1
})
val
})
}
#Main UI
ui <-
tagList(
tags$style(HTML(
paste(
"html,",
".container{
width: 100%;
margin: 0 auto;
padding: 0;
}
@media screen and (min-width: 700px){
.container{
min-width: 1850px;
max-width: 1920px;
}
}
",
sep = " "
)
)),
tags$div(
class = "container",
dashboardPage(
dashboardHeader(disable = TRUE),
dashboardSidebar(disable = TRUE),
dashboardBody(
#UI for download
fluidRow(column(
8, offset = 2, box(
solidHeader = TRUE,
column(6, align = 'right', class = 'download_padding', downloadButton(
outputId = "download",
label = "Download Report",
class = 'download_button',
width = 150,
)
),
width = 16,
)
)),
#End of UI for download
#UI for Trade Agreements
trade_agreement_ui(id = "agreement")
#End of UI Trade Agreements
)
)
)
)
#### End Create User Interface #####
#Main Server
#### Create Server actions #####
server <- shinyServer(function(input, output, session) {
#### Trade Agreements ####
v1 <- trade_agreement_server(
id = "agreement"
)
#### Trade Agreements ####
output$download <- downloadHandler(
filename = function() {
paste0("plot.pdf")
},
content = function(file) {
pdf(file)
v1$map1
dev.off()
}
)
})
#### End create Server actions #####
#### Run application #####
shinyApp(ui, server)
#### End Run application #####