]> git.vanrenterghem.biz Git - www2.vanrenterghem.biz.git/blob - source/posts/using_R_to_automate_reports.org
Gebruik juist filetags formaat.
[www2.vanrenterghem.biz.git] / source / posts / using_R_to_automate_reports.org
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:
14 #+BEGIN_SRC R
15 :PROPERTIES:
16 :CUSTOM_ID: format-r
17 :END:
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.
24 ##Add a title.
26 Some further text.
28 ={r, echo=FALSE} plot(my-plot-object) kable(my-df-or-table)=
29 #+END_SRC
31 When you are ready to create the report, the convenience of
32 [[https://cran.r-project.org/web/packages/rmarkdown/][RMarkdown]] is
33 hard to beat:
35 #+BEGIN_SRC bash 
36 R -e "rmarkdown::render('_{/my-report.Rmd',output_file='}/my-report.pdf')"
37 #+END_SRC
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=.