title: How to pass "reactive" data into R Markdown as a parameter?
body: <p>I want to be able to pass a reactive <code>data.table</code> to my Rmarkdown document that will sort the table and print the top, say 5, sorted entries. </p>
  
  <p>I keep running into "Error : x must be a data.frame or data.table" at <code>setorderv</code>. I suspect the reactive is upsetting the <code>is.data.table</code> checks. </p>
  
  <p>How do I simply pass a copy of the data to the Rmd document? And remove the reactivity wrapper?</p>
  
  <p>Here is a simplified example cobbled from reactivity and downloadHandler stock examples. I took at look at <a href="https://stackoverflow.com/questions/38431883/feeding-a-reactive-variable-in-an-r-markdown-document">this on SO</a> which suggested using observe but I am not sure how to implement this. Thanks I appreciate some help on this. </p>
  
  <p><strong>RMARKDOWN - SOtestreport.Rmd</strong> </p>
  
  <pre><code>---
  title: "Top in Segment Report"
  output: html_document
  params:
    n: 5
    dt: NA
    top.field: NA
  ---
  
  
  ```{r setup, echo = FALSE}
  library(data.table)
  library(knitr) # for kable
  
  ```
  Data report as at: `r format(Sys.time(), "%a %b %d %Y %X")`
  
  ```{r, echo = FALSE}
  table.str &lt;- paste0("Showing Top ", params$n ," tables for these fields: ", 
               params$top.field)
  
  # is this the best way of having dynamic labels?
  ```
  `r table.str`
  
  
  ```{r, echo = FALSE}
  # tried this
  # test.dt &lt;- copy(dt)
  
  # normally a function GenerateTopTable with limit sort field etc.
  setorderv(dt, params$top.field, -1) # -1 is descending. THIS IS WHERE IT CRASHES
  out &lt;- dt[1:params$n,]
  
  ```
  
  
  ```{r toptable, echo = FALSE}
  
  kable(out, digits = 3)
  
  ```
  </code></pre>
  
  <p><strong>app.R</strong></p>
  
  <pre><code># SO test report
  # mini example based on https://shiny.rstudio.com/gallery/reactivity.html
  
  library(shiny)
  library(datasets)
  
  server &lt;- function(input, output) {
  
    datasetInput &lt;- reactive({
      switch(input$dataset,
             "rock" = rock,
             "pressure" = pressure,
             "cars" = cars)
    })
  
    output$caption &lt;- renderText({
      input$caption
    })
  
    output$summary &lt;- renderPrint({
      dataset &lt;- datasetInput()
      summary(dataset)
    })
  
    output$view &lt;- renderTable({
      head(datasetInput(), n = input$obs)
    })
  
  
    # PROBLEM BIT WHERE I AM ADDING ON GALLERY EXAMPLE
    # handler from https://stackoverflow.com/questions/37347463/generate-report-with-shiny-app-and
    output$topreport &lt;- downloadHandler(
      filename = "topreport.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 &lt;- file.path(tempdir(), "SOtestreport.Rmd")
        file.copy("SOtestreport.Rmd", tempReport, overwrite = TRUE)
  
        # Set up parameters to pass to Rmd document
        #  TRIED THIS TOO
        #  my.dt &lt;- as.data.table(datasetInput())
        my.dt &lt;- datasetInput()
        my.params &lt;- list(n = input$obs, 
                          dt = my.dt,
                          top.field = names(my.dt)[1])
  
        rmarkdown::render(tempReport, output_file = file,
                          params = my.params,
                          envir = new.env(parent = globalenv())
        )
      }
    )
  </code></pre>