title: Generating downloadable reports from Shiny app
body: <p>I made an R script that allows to get an R Markdown report with a certain type of dataset. Now I would like other people to be able to use this script in order to get an automated report with their data but without using this script (especially for people who don't master R).</p>
  <p>I try to go through Shiny hoping to make an interface that loads a dataset and would make my script automatically but I can't make the link between Shiny and my Rmd.</p>
  <p>How can I tell my Rmd that the dataset to be processed is not the one that my Rmd script was going to look for in a directory but the one that was loaded on the Shiny interface?</p>
  <p>Thanks</p>
  <p>Here is the Shiny script with my Rmd called &quot;traitemant_bis.Rmd&quot; :</p>
  <pre><code>library(shiny)
  library(rmarkdown)
  
  ui &lt;- fluidPage(
    titlePanel(&quot;Uploading Files&quot;),
    sidebarLayout(
      sidebarPanel(
        fileInput(
          inputId = &quot;file1&quot;, label = &quot;Choose CSV File&quot;,
          multiple = FALSE,
          accept = c(&quot;text/csv&quot;, &quot;text/comma-separated-values,text/plain&quot;, &quot;.csv&quot;)
        ),
        radioButtons(&quot;format&quot;, &quot;Document format&quot;, c(&quot;PDF&quot;, &quot;HTML&quot;, &quot;Word&quot;), inline = TRUE)
      ),
      mainPanel(
        tableOutput(&quot;contents&quot;),
        downloadButton(&quot;downloadReport&quot;)
      )
    )
  )
  
  
  server &lt;- function(input, output) {
    dataset &lt;- reactive({
      req(input$file1)
      read.csv(file = input$file1$datapath,
               na.strings = &quot;.&quot;, 
               sep = &quot;;&quot;,
               header = TRUE,
               nrows=10)               
    })
    
    output$contents &lt;- renderTable({
      req(dataset())
      head(dataset())
    })
    
    output$downloadReport &lt;- downloadHandler(
      filename = function() {
        paste(&quot;my-report&quot;, sep = &quot;.&quot;, switch(
          input$format, PDF = &quot;pdf&quot;, HTML = &quot;html&quot;, Word = &quot;docx&quot;
        ))
      },
      
      content = function(file) {
        src &lt;- normalizePath(&quot;traitemant_bis.Rmd&quot;)
        
        owd &lt;- setwd(tempdir())
        on.exit(setwd(owd))
        file.copy(src, &quot;traitemant_bis.Rmd&quot;, overwrite = TRUE)
        
        out &lt;- render(&quot;traitemant_bis.Rmd&quot;, switch(
          input$format,
          PDF = pdf_document(), HTML = html_document(), Word = word_document()
        ))
        file.rename(out, file)
      }
    )
  }
  
  shinyApp(ui, server) ```
  </code></pre>