Site icon R-bloggers

Useful YAML options for generating HTML reports in R

[This article was first published on R | Dr. Atakan Ekiz, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I think most people start using R with scripts initially. Scripts are great for modular computational tasks, but for generating informative reports, Rmarkdown is a must. Rmd makes it possible to use a YAML header to specify certain parameters right at the beginning of the document. Built-in YAML parameters make it easier to create more organized and informative reports. In this post, I will share with you few of the YAML options I commonly use for generating HTML outputs in my data analysis projects. You can check out the following resources for details about tons of other options:

YAML header in an Rmd file looks like this:

---
title: My analysis file
author: Atakan Ekiz
date: August 6, 2020 # can execute R code here (see below)
output: html_document
---

Below you can find some general and HTML output-specific YAML options. However, Rmarkdown can also be used to generate other types of reports (such as PDF, Word, Powerpoint, etc.) (see more here)



General YAML metadata options

author:
- Name_1 # can be indented or not
- Name_2 # but be consistent among different entries
author:
- name: Name_1 # can list separate items
affiliation: University of Someplace # same amt of indentation needed
- name: Name_2
affiliaion: University of SomeOtherPlace
date: "`r format(Sys.time(), '%d %B, %Y')`" # nicely formatted
date: "`r lubridate::today()`" # can use functions from pkgs
abstract: |
First paragraph of what may be the best abstract ever written. It is a
beautiful abstract because nobody writes abstracts better than I do.
Second paragraph of the abstract continues to be just magnificent. It is
simply because I'm a very stable genius. OK, SERIOUS NOW: HTML code can be
also added to leave some breating space after abstract such as <br><br><br>



html_document specific YAML options

These variables need to be indented under the html_document option. Some of my favorites are:

includes:
in_header: header.html
before_body: doc_prefix.html
after_body: doc_suffix.html

Finally, a complete YAML example:

---
title: Our groundbreaking analysis that will lead to world peace
author:
- name: John, the Problem Solver
affiliation: Get Shiz Done University
- name: Jack, the Supposed Contributor
affiliation: Living Room Couch
date: "`r lubridate::today()`"
abstract: |
Our analysis shows that the main reason of global conflicts can be attributed
to the insufficient amounts of Nutella consumption in situation rooms.
It turns out that the government officials across the globe are a bit grumpy
when they get hungry leading to bad decision-making. You are welcome...
<br><br><br>
output:
html_document:
toc: true
toc_float: true
toc_depth: 2
number_sections: true
code_folding: hide
theme: readable
---

Rmarkdown is just awesome! I would say almost as good as Nutella! I feel like I keep learning hidden (?) features of R and Rmarkdown every day. For instance, I just realized that subheaders in the HTML report can be shown under different tabs on the same page. The result is a gorgeous and neatly organized HTML report! I’m sure the awesomeness doesn’t end there. Let me know if you have other favorite features in Rmarkdown/YAML to streamline your analyses and increase your productivity.

To leave a comment for the author, please follow the link and comment on their blog: R | Dr. Atakan Ekiz.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.