title: Plotly to pdf report
body: <p>I'd like to add a plotly plot to a pdf file. This should be done locally (without plotly_IMAGE). Unfortunately I do not have access to webshot (since I do not have the rights to install PhantomJS). Therefore I tried a workaround described here: <a href="https://github.com/ropensci/plotly/issues/311" rel="noreferrer">https://github.com/ropensci/plotly/issues/311</a> using shiny gadgets. But I wanted to adapt it such that the copying is doen without clicking done in the gadget.</p>
  
  <p>All my attempts (see below) failed because I was not able to delay the downloadHandler until the copying was done.</p>
  
  <p>Any suggestions how the dresired result (copy plotly-Image as png first and use it in pdf-Export afterwards) all done sequentially after click on Download Button?</p>
  
  <p>ui:</p>
  
  <pre><code>library(shiny)
  library(plotly)
  library(rsvg)
  
  
  shinyUI(fluidPage(
    title = 'Download a PDF report',
    sidebarLayout(
  
      sidebarPanel(
        helpText(),
        selectInput('x', 'Build a regression model of mpg against:',
                    choices = names(mtcars)[-1]),
        radioButtons('format', 'Document format', c('PDF', 'HTML', 'Word'),
                     inline = TRUE),
        downloadButton('downloadReport'),
        tags$script('
         document.getElementById("downloadReport").onclick = function() {
            var plotly_svg = Plotly.Snapshot.toSVG(
              document.querySelectorAll(".plotly")[0]
        );
  
                    Shiny.onInputChange("plotly_svg", plotly_svg);
            };
         ')
      ),
      mainPanel(
        plotlyOutput('regPlot')
      )
    )
  ))
  </code></pre>
  
  <p>server:</p>
  
  <pre><code>library(shiny)
  library(plotly)
  library(rsvg)
  
  shinyServer(function(input, output, session) {
  
    output$regPlot &lt;- renderPlotly({
      library(plotly)
      set.seed(100)
      d &lt;- diamonds[sample(nrow(diamonds), 1000), ]
      print("render")
      p &lt;- plot_ly(d, x = carat, y = price, text = paste("Clarity: ", clarity),
              mode = "markers", color = carat, size = carat)
      p
    })
  
    observeEvent(input$plotly_svg, priority = 10, {
      png_gadget &lt;- tempfile(fileext = ".png")
      png_gadget &lt;- "out.png"
      print(png_gadget)
      rsvg_png(charToRaw(input$plotly_svg), png_gadget)
    })
  
    output$downloadReport &lt;- downloadHandler(
      filename = function() {
        paste('my-report', sep = '.', switch(
          input$format, PDF = 'pdf', HTML = 'html', Word = 'docx'
        ))
      },
  
      content = function(file) {
        src &lt;- normalizePath('testreport.Rmd')
  
        # temporarily switch to the temp dir, in case you do not have write
        # permission to the current working directory
        owd &lt;- setwd(tempdir())
        on.exit(setwd(owd))
        file.copy(src, 'testreport.Rmd')
  
        library(rmarkdown)
        out &lt;- render('testreport.Rmd', params = list(region = "Test"), switch(
          input$format,
          PDF = pdf_document(), HTML = html_document(), Word = word_document()
        ))
        file.rename(out, file)
      }
    )
  })
  </code></pre>