]> git.vanrenterghem.biz Git - www2.vanrenterghem.biz.git/blob - posts/using_R_to_automate_reports.mdwn
Verplaats grafieken.
[www2.vanrenterghem.biz.git] / posts / using_R_to_automate_reports.mdwn
1 [[!meta date="2016-10-10 21:48:11 +0800"]]
3 [[!tag R code automation]]
4 A lot of information on [knitr](https://cran.r-project.org/web/packages/knitr/index.html) is centered around using it for reproducible research. I've found it to be a nice way to make abstraction of mundane reporting though. It is as easy as performing the necessary data extraction and manipulation in an R script, including the creation of tables and graphs.
6 To develop the report template, simply `source` the R script within an Rmd one, per the example template below:
8 [[!format r """
9 ---
10 title: "My report"
11 date: "`r Sys.time()`" 
12 output: pdf_document
13 ---
15 ```{r setup, include=FALSE}
16 library(knitr)
17 knitr::opts_chunk$set(echo = TRUE)
18 source('my-report.R')
19 ```
21 Include some text about your report.
23 ##Add a title.
25 Some further text.
27 ```{r, echo=FALSE}
28 plot(my-plot-object)
29 kable(my-df-or-table)
30 ```
31 """]]
33 When you are ready to create the report, the convenience of [RMarkdown](https://cran.r-project.org/web/packages/rmarkdown/) is hard to beat:
35 [[!format bash """
36 R -e "rmarkdown::render('~/my-report.Rmd',output_file='~/my-report.pdf')"
37 """]]
39 Thanks to the YAML header at the start of the report template, information like the report's title and target output format don't need to be mentioned. This command can easily be scripted a bit further to include a date-time stamp in the output filename for instance, and scheduled using `cron`.