How to download several plots as pdf in a shiny app with only one download button


#1

Hello I have a shiny app which creates 2 plots. These 2 plots use as inputs the same values from the sidebar. For the needs of this example I duplicated exactly the same plot but it makes no difference with my real case as the inputs will be still the same for both.I want to be able to download both by clicking on one download button.

 ui <- fluidPage(
        
        # App title ----
        titlePanel("Hello Shiny!"),
        
        # Sidebar layout with input and output definitions ----
        sidebarLayout(
          
          # Sidebar panel for inputs ----
          sidebarPanel(
            
            # Input: Slider for the number of bins ----
            sliderInput(inputId = "bins",
                        label = "Number of bins:",
                        min = 1,
                        max = 50,
                        value = 30),
            downloadButton(outputId = "plots", label = "Download the plots")
            
          ),
          
          # Main panel for displaying outputs ----
          mainPanel(
            
            # Output: Histogram ----
            plotOutput(outputId = "distPlot"),
            plotOutput(outputId = "distPlot2")
            
            
          )
        )
      )
      # Define server logic required to draw a histogram ----
      server <- function(input, output) {
        
        # Histogram of the Old Faithful Geyser Data ----
        # with requested number of bins
        # This expression that generates a histogram is wrapped in a call
        # to renderPlot to indicate that:
        #
        # 1. It is "reactive" and therefore should be automatically
        #    re-executed when inputs (input$bins) change
        # 2. Its output type is a plot
        
        
        output$distPlot <- renderPlot({
          
          x    <- faithful$waiting
          bins <- seq(min(x), max(x), length.out = input$bins + 1)
          hist(x, breaks = bins, col = "#75AADB", border = "white",
               xlab = "Waiting time to next eruption (in mins)",
               main = "Histogram of waiting times")
          
        })
        output$distPlot2 <- renderPlot({
          
          x    <- faithful$waiting
          bins <- seq(min(x), max(x), length.out = input$bins + 1)
          hist(x, breaks = bins, col = "#75AADB", border = "white",
               xlab = "Waiting time to next eruption (in mins)",
               main = "Histogram of waiting times")
          
        })
        output$plots <- downloadHandler(
          filename = function() {
            "Rplots.pdf"
          },
          content = function(file) {
            pdf(file)
            print(
              x    <- faithful$waiting,
              bins <- seq(min(x), max(x), length.out = input$bins + 1),
              hist(x, breaks = bins, col = "#75AADB", border = "white",
                   xlab = "Waiting time to next eruption (in mins)",
                   main = "Histogram of waiting times")
            )
            print( x    <- faithful$waiting,
                   bins <- seq(min(x), max(x), length.out = input$bins + 1),
                   hist(x, breaks = bins, col = "#75AADB", border = "white",
                        xlab = "Waiting time to next eruption (in mins)",
                        main = "Histogram of waiting times")) 
            
            dev.off()
          }
        )   
        
      }
      shinyApp(ui, server)

#2

Hi,

you can use markdown to create a PDF file containing your plots. Then you can use a single download button to download the PDF.