2

I am trying to generate a report in PDF out of a Shiny App. I have two issues:

  1. Report not downloading in PDF, just HTML. How do I download it on PDF?
  2. Report not showing what was rendered (ggplotly image in this case), just showing text. How do I make that PDF to contain the images shown in the Shiny App?

What am I doing wrong? Any ideas?

library(ggplot2)
library(plotly)
library(gapminder)
library(shiny)
library(shinydashboard)
library(rmarkdown)

bodyplotly <- dashboardBody(
  fluidRow(
    column(12,
           box(width=10,
               title = "ggplotly", 
               plotlyOutput("selected_plot"),
               box(
                 width = 2, height=250,
                 sliderInput("year", label=("Choose year"),
                             min = 1950, max = 2000, value = 1977),
                 downloadButton("report", "Generate report")
           )))))  
  
ui <- navbarPage("Test",tabPanel("plotly",
                          bodyplotly))

server <- function(input, output,session) {
  
  output$selected_plot <- renderPlotly({
    
    p <- gapminder %>%
      filter(year==input$year) %>%
      ggplot( aes(gdpPercap, lifeExp, size = pop, color=continent)) +
      geom_point() +
      theme_bw()
    
    ggplotly(p)
    
    
  })
  
  output$downloadreport <- downloadHandler(
    # For PDF output, change this to "report.pdf"
    filename = "report.html",
    content = function(file) {
      # Copy the report file to a temporary directory before processing it, in
      # case we don't have write permissions to the current working dir (which
      # can happen when deployed).
      tempReport <- file.path(tempdir(), "report.Rmd")
      file.copy("report.Rmd", tempReport, overwrite = TRUE)
      library(rmarkdown)
      # Set up parameters to pass to Rmd document
      params <- list(input$year)
      # Knit the document, passing in the `params` list, and eval it in a
      # child of the global environment (this isolates the code in the document
      # from the code in this app).
      rmarkdown::render(tempReport, pdf_document(),
                        #output_options=self_contained,
                        #params = params,
                        envir = new.env(parent = globalenv())
      )
    
    }
  )
  
  }
  
  shinyApp(ui = ui, server = server)
MrFlick
  • 195,160
  • 17
  • 277
  • 295
user284437
  • 129
  • 5
  • 2
    Is it because the file name is “report.html”? – Joe Jul 26 '20 at 00:35
  • 2
    Also, shouldn’t ***output$downloadreport*** be ***output$report*** instead? – Joe Jul 26 '20 at 00:37
  • Good catch. I did both but it's still not working. – user284437 Jul 26 '20 at 00:40
  • 3
    What is the yaml header in your template .Rmd? Could you be defining a html file here? – Paul Jul 26 '20 at 01:12
  • I’m not familiar with downloadHandler, but in the example at this link, content is a function of file, and that formal parameter is used in the body of the function, whereas I don’t see ***file*** in the body of your function https://shiny.rstudio.com/reference/shiny/latest/downloadHandler.html – Joe Jul 26 '20 at 02:36
  • 2
    Also, why are the lines with output_options and params commented out? – Joe Jul 26 '20 at 02:38
  • The issue was that I had not set up the .Rmd file correctly. Thanks for the help guys. – user284437 Jul 26 '20 at 03:50

0 Answers0