Downloadable reports with knitr

This same function can very easily allow your users to produce custom reports in HTML, pdf, or MS Word ready to be downloaded to their machines, using the knitr package (http://yihui.name/knitr/). knitr is a user-contributed package that allows reports to be generated dynamically from a mixture of a static report formats interleaved with the output from of R.

So, for example, titles and text can be fixed, but each time the report is run, a different output will be produced within the document depending on the state of the data when the output is generated. knitr can be used with the R Markdown format. Here is the simple R Markdown document within the Google Analytics application:

# Summary report  
## Text summary  
This report summarises data between `r strftime(input$dateRange[1], 
format = "%d %B %Y")` and `r strftime(input$dateRange[2],
format = "%d %B %Y")`.
## Trend graph ```{r fig.width=7, fig.height=6, echo=FALSE} trendGraph() ```

As can be seen, the document is a mix of static headings and text, inline R output (given as `r "print("somthing")`), and graphical output. The trendGraph() function, of course, is the same trendGraph() function that we saw in the download graphics code.

The code to download the report is as follows (with the R Markdown document in the same folder as server.R and named Report.Rmd):

output$downloadDoc <-  
  downloadHandler(filename = "Report.html",  
    content = function(file){  
      knit2html("Report.Rmd", envir = environment())  
 
      # copy document to 'file'  
      file.copy("Report.html", file,  
        overwrite = TRUE)  
    }  
  )  

Adding a button to download the graph is the same as for the downloading graph function; the following should be placed in ui.R within the sidebarPanel() function:

downloadButton("downloadDoc", "Download report")  
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset