library(DT)
library(data.table)
Attaching package: 'data.table'
The following object is masked from 'package:base':
%notin%
library(knitr)
library(ggplot2)
suppressPackageStartupMessages(library(plotly))
knitr::opts_chunk$set(echo = TRUE)Philipp Probst
December 2, 2023
In this blog post I will explain how you can automatise the creation of R-Markdown documents.
My main motivation for this was that I had a document where I had to create an R Markdown with several very similar sections. Of course I could write each section with new code, but I do not like repetive tasks (they are boring and tiring), so I decided to automatise the creation of sections, tables and graphs.
I do not want to run all the (quite long) data creation process when building the R-Markdown. So firstly I created a script to create the data and saved it with RDS format (I am also a big fan of fst format, but the data was in a nested list, which cannot be saved in fst-Format).
When creating the R-Markdown the data was ready to use.
The next step was to create the introduction of the document. I wanted to put all the creation of the outputs into functions.
First thing is to load some of the necessary libraries
So my way to go was to use the option results = "asis" in the setting of R-Markdown:
This option tells knitr not to wrap your text output in verbatim code blocks, but treat it “as is.”
Output:
Sepal.Length
Sepal.Width
Petal.Length
Petal.Width
Species
It is important (and a bit laborious) to wrap outputs into cat, otherwise it is not treated correctly by R-Markdown. Moreover \n\n is used to start a new line. Here a bad example:
Output:
[1] “- Sepal.Length” “- Sepal.Width” “- Petal.Length” [4] “- Petal.Width” “- Species”
It is important to set the option echo = FALSE, otherwise the input code is also printed out. Some introduction with header could look like this:
Output:
Model creator:
Philipp Probst
Creation Date of Models:
[1] “2026-10-26”
As you can see in the above code I used rknitr::knit_print` to have a proper output for the date. This will be necessary in many cases (e.g. also when printing tables or plots).
Moreover to show things by using the space ideally and in a nice way is to use tabs.
For this we have to add \{.tabset \} behind the header. \{.unnumbered \} can be used to set off numbering of the header, if in general it is turned on (e.g. in the preamble: number_sections: true).
Output:
ABC
DEF
Another nice tool is that you can fold some section, text, graphs, etc.
For this you have to use the HTML <details> tag:
Output:
Some text
DEF
For printing tables it is necessary to wrap the function knitr::knit_print() and DT::datatable around them. Then it has also nice sorting abilities.
It is also possible to print a table by using print(kable(…)) although it does not have the nice abilities of DT::datatable .
Output:
| Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species |
|---|---|---|---|---|
| 5.1 | 3.5 | 1.4 | 0.2 | setosa |
| 4.9 | 3.0 | 1.4 | 0.2 | setosa |
| 4.7 | 3.2 | 1.3 | 0.2 | setosa |
| 4.6 | 3.1 | 1.5 | 0.2 | setosa |
| 5.0 | 3.6 | 1.4 | 0.2 | setosa |
| 5.4 | 3.9 | 1.7 | 0.4 | setosa |
Base-R, ggplot2 or plotly plots do not need any adjustment in the results = "asis" mode.
Output:


I will add more tricks in the next blog post.
E.g. it will include the following