title: How to make pdf download in shiny app response to user inputs?
body: <p>I want to make the table and the barplot generated by my shiny app to be downloadable as a pdf report. I can generate the report with the selected inputs the first time I start the app on my local computer, but when I switch the inputs, it doesn't generate the reports of the new inputs on pdf.  </p>
  
  <p>Here's my ui code  </p>
  
  <pre><code>require(shiny)
  require(shinydashboard)
  require(ggplot2)
  require(ggthemes)
  
  sample &lt;- read.csv("new_sample2.csv", stringsAsFactors = FALSE)
  
  header &lt;- dashboardHeader(title = "XYZ School Student Dashboard", titleWidth = 370)
  
  body &lt;- dashboardBody(
  tags$head(tags$style(HTML('
    .main-header .logo {
                          font-family: "Georgia", Times, "Times New Roman", serif;
                          font-weight: bold;
                          font-size: 20px;
                          }
                          '))),
  fluidRow(
  column(width = 9,
  box(title = "Selected Student", width = NULL, solidHeader = TRUE, status = "info",
             textOutput("summary1"),
             textOutput("summary2"),
             textOutput("summary3")
  ),
  
         box(title = "Marks card", status = "info", width = NULL, solidHeader = TRUE, collapsible = TRUE,
             tableOutput("table")),
         box(title = "Marks card bar plot", status = "info", width = NULL, solidHeader = TRUE, collapsible = TRUE,
             plotOutput("plot"))
  ),
  
  column(width = 3,
         box(title = "Select", background = "blue" ,width = NULL,
             selectInput("class", "Class", unique(sample$class)),
             selectInput("name", "Name", unique(sample$name)),
             selectInput("exams", "Exams", choices = c("1st Periodic Test", "1st Term", "2nd Periodic Test",
                                                       "2nd Term", "3rd Periodic Test", "4th Periodic Test",
                                                       "Final")),
  
             "Note: In the Bar Plot", 
             br(),
             "1. The black line is the average class mark for that particular subject.",
             br(),
             "2. The red line is the pass mark for that particular subject.",
             hr(),
             downloadButton("downloadReport", "Download report")
             )
         )
    )
  )
  
  
  ui &lt;- dashboardPage(skin = "blue",
      header,
        dashboardSidebar(disable = TRUE),
          body
  )  
  </code></pre>
  
  <p>And here's my server code  </p>
  
  <pre><code>server &lt;- function(input, output, session){
  
  output$summary1 &lt;- renderText({
  paste("Student Name: ", input$name)
  })
  
  output$summary2 &lt;- renderText({
  paste("Class: ", input$class)
  })
  output$summary3 &lt;- renderText({
  paste("Examination: ", input$exams)
  })
  
  
  getdataset &lt;- reactive({
  dataset &lt;- sample[sample$class == input$class &amp; sample$name == input$name &amp; sample$examination == input$exams, ]
  })
  
  observe({
  classInput &lt;- input$class
  updateSelectInput(session, "name", choices = sample$name[sample$class == classInput])
  })
  
  output$table &lt;- renderTable({
  dataset &lt;- getdataset()
  dataset[, c("date", "subject", "maximum_mark", "pass_mark", "obtain_mark", "class_ave", "pc", "exam_pc")]
  })
  
  plotInput &lt;- reactive({
  df &lt;- getdataset()
  ggplot(df, aes(x = subject, y = obtain_mark)) +
    theme_fivethirtyeight() +
    geom_bar(stat = "identity", fill = "#006699") +
    geom_text(aes(label = obtain_mark),vjust = -0.4) +
    geom_errorbar(data = getdataset(),
                  aes(y = class_ave, ymax = class_ave,
                      ymin = class_ave), colour = "#000000") +
    geom_errorbar(data = getdataset(),
                  aes(y = pass_mark, ymax = pass_mark,
                      ymin = pass_mark), colour = "red") +
    labs(title = paste(input$name,"'s", input$exams, "marks"), x = "", y = "Marks") +
    theme(axis.text=element_text(size=10, face = "bold")
    )
  })
  
  output$plot &lt;- renderPlot({
  print(plotInput())
   })
  
  output$downloadReport &lt;- downloadHandler(
  filename = "Student-report.pdf",
  content = function(file){
    inputEnv &lt;- new.env()
    inputEnv$class &lt;- input$class
    inputEnv$name &lt;- input$name
    inputEnv$exams &lt;- input$exams
    inputEnv$data &lt;- getdataset()
    out = rmarkdown::render("student_report.Rmd", envir = inputEnv)
    file.rename(out, file)
       }
      )
     }
  
   shinyApp(ui, server)  
  </code></pre>
  
  <p>This is the .Rmd file that I have placed in the same folder where app.R is.  </p>
  
  <pre><code>---
  title: "school_report"
  author: "Management"
  date: "May 4, 2016"
  output: pdf_document
  ---
  
  ```{r echo=FALSE}
  plotInput()
  ```  
  
  ```{r echo=FALSE}
  dataset &lt;- getdataset()
  dataset[, c("date", "subject", "maximum_mark", "pass_mark", "obtain_mark", "class_ave", "pc", "exam_pc")]
  ```  
  </code></pre>
  
  <p>The data is a sample of marks scored by students in exams conducted by the school.  </p>
  
  <pre><code>head(sample)
   class   name       examination       date        subject maximum_mark pass_mark obtain_mark  pc class_ave
  1   1 Adison 1st Periodic Test 2015-03-23      English-I        20         8          14     70      15
  2   1 Adison 1st Periodic Test 2015-03-24    Mathematics        20         8          19     95      16
  3   1 Adison 1st Periodic Test 2015-03-25        Science        20         8          18     90      12
  4   1 Adison 1st Periodic Test 2015-03-26          Hindi        20         8          20    100      15
  5   1 Adison 1st Periodic Test 2015-03-27 Social Studies        20         8          19     95      11
  6   1 Adison 1st Periodic Test 2015-03-28            M.M        20         8          20    100      14
   exam_pc
  1 92.86
  2 92.86
  3 92.86
  4 92.86
  5 92.86
  6 92.86  
  
  tail(sample)
       class   name examination       date       subject maximum_mark pass_mark obtain_mark  pc class_ave
  1851   2   Denver       Final 2015-12-10    English-II          100        40          93  93        59
  1852   2   Denver       Final 2015-12-02       Drawing           50        20          25  50        34
  1853   2   Denver       Final 2015-11-30            GK           50        20          50 100        42
  1854   2   Denver       Final 2015-12-01 Moral Science           50        20          50 100        41
  1855   2   Denver       Final 2015-12-02     Dictation           25        10          25 100        20
  1856   2   Denver       Final 2015-11-30  Hand Writing           25        10          25 100        20
         exam_pc
   1851   87.89
   1852   87.89
   1853   87.89
   1854   87.89
   1855   87.89
   1856   87.89  
  </code></pre>
  
  <p>I would really appreciate your help.</p>