Plotly embedded into Shiny and in RMarkdown
<p>I would like to download Report within Shiny App, which includes <code>Plotly</code> graph.
So far i have not found any answer on stackoverflow. 
Till this moment im able to download the screenshot of <code>Plotly</code> but it <strong>appears only in my working directory</strong> and it is not sent to <code>Rmarkdown</code>.</p>

<p>Example code:</p>

<pre><code>library(shiny)
library(plotly)
library(rsvg)
library(ggplot2)

d &lt;- data.frame(X1=rnorm(50,mean=50,sd=10),X2=rnorm(50,mean=5,sd=1.5),Y=rnorm(50,mean=200,sd=25))

ui &lt;-fluidPage(
  title = 'Download report',
  sidebarLayout(

    sidebarPanel(
      helpText(),
      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')
    )
      )
)

server &lt;- function(input, output, session) {

  output$regPlot &lt;- renderPlotly({
    p &lt;- plot_ly(d, x = d$X1, y = d$X2,mode = "markers")
    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')

      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)
    }
  )
}

shinyApp(ui = ui, server = server)
</code></pre>

<p>and testreport.Rmd file:</p>

<pre><code>---
title: "test"
output: pdf_document
params:
  name: "Test"
  region: 'NULL'
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
</code></pre>

<p>Any help would be appreciated, because there is not many sources and documentations about <code>R Plotly</code>.</p>

<p>Cheers</p>