1 #+date: <2016-10-10 21:48:11 +0800>
2 #+title: Using R to automate reports
3 #+filetags: :R:code:automation:
5 A lot of information on [[https://cran.r-project.org/web/packages/knitr/index.html][knitr]] is
6 centered around using it for reproducible research. I've found it to be
7 a nice way to make abstraction of mundane reporting though. It is as
8 easy as performing the necessary data extraction and manipulation in an
9 R script, including the creation of tables and graphs.
11 To develop the report template, simply =source= the R script within an
12 Rmd one, per the example template below:
18 title: "My report" date: "=r Sys.time()=" output: pdf_document ---
20 ={r setup, include=FALSE} library(knitr) knitr::opts_chunk$set(echo = TRUE) source('my-report.R')=
22 Include some text about your report.
28 ={r, echo=FALSE} plot(my-plot-object) kable(my-df-or-table)=
31 When you are ready to create the report, the convenience of
32 [[https://cran.r-project.org/web/packages/rmarkdown/][RMarkdown]] is
36 R -e "rmarkdown::render('_{/my-report.Rmd',output_file='}/my-report.pdf')"
39 Thanks to the YAML header at the start of the report template,
40 information like the report's title and target output format don't need
41 to be mentioned. This command can easily be scripted a bit further to
42 include a date-time stamp in the output filename for instance, and
43 scheduled using =cron=.