title: Using plots and data tables from Shiny app in R markdown report
body: <p>I have created a Shiny app, which involves the user importing data, manipulating it, creating a datatable and associated plot. </p>
  
  <p>I'd like to build in a downloadable report using Rmarkdown (which I've only just started to use). However, i am unsure how to print the datatables and plots generated in R, in the Rmarkdown script, without copying the whole R code from the Shiny app. It is quite a long piece of code, so i'd like to be able to use the outputs directly. </p>
  
  <p>As an example, I've copied the following app to demonstrate my point.</p>
  
  <pre><code>library(shiny)
  
  ui &lt;- fluidPage(
  
     titlePanel("Old Faithful Geyser Data"),
  
     sidebarLayout(
        sidebarPanel(
           sliderInput("bins",
                       "Number of bins:",
                       min = 1,
                       max = 50,
                       value = 30)
        ),
  
        mainPanel(
           plotOutput("distPlot"),
           dataTableOutput("summary_table"),
           downloadButton("report", "Generate report")
        )
  
     )
  )
  
  server &lt;- function(input, output) {
  
     output$distPlot &lt;- renderPlot({
        x    &lt;- faithful[, 2] 
        bins &lt;- seq(min(x), max(x), length.out = input$bins + 1)
  
        hist(x, breaks = bins, col = 'darkgray', border = 'white')
     })
  
     output$summary_table &lt;- renderDataTable({
       x &lt;- faithful
       head(x,5)
     })
  
     output$report &lt;- downloadHandler(
       filename = "report.html",
       content = function(file) {
         tempReport &lt;- file.path(tempdir(), "report.Rmd")
         file.copy("report.Rmd", tempReport, overwrite = TRUE)
         rmarkdown::render(tempReport, output_file = file,
                           envir = new.env(parent = globalenv())
         )
       }
     )
  }
  
  shinyApp(ui = ui, server = server)
  </code></pre>
  
  <p>I would like to access the plot and data table in my downloadable R markdown file.</p>