11 How to create a dynamic report
Suppose we are requested to produce a report. This report has the following features.
- It is based on data collected quarterly (or annually). That is, this is a report that provides the context and summarises the data with texts, numbers, graphs and tables.
- The report must be quickly updated when the new quarterly (or annually) data are available—this is a dynamic report.
How to create such a report? The short answer is that we can use the Knitr package to do it.
The workflow is shown below.
- Create a new project, called e.g. DynamicReport in Rstudio, and then create subfolders: Data, R, Figures, Tables, Knitr, Output
- Save raw data in the Data folder.
- Write R programs and save them in the R folder. The R programs are for data analysis, such as tidying up the data (if necessary), creating graphs (the created graphs will be saved in the Figures folder), tables (will be saved in the Tables folder in TEX format) and numbers (will be saved in an Rdata file which will be in an automatically created sub-folder called “cache” under the R folder).
- Prepare Report.Rnw file, which will be saved in the Knitr folder.
- Create integrate.R (under the project folder) to produce the report.
Following the above workflow, I have done an example called “dynamic_report”, and this example can be downloaded from my GitHub page https://github.com/LarryZhang2016