Section 16 More on R Markdown

16.1 Tables

There are several options for producing tables in R Markdown.

16.1.1 Using Markdown

Here’s an example of making a table with the Markdown syntax. Note the use of : for specifying text alignment.

| column 1  | column 2  |  column 3 |
|:----------|:---------:|----------:|
| this text | this text | this text |
| is left   | is centre | is right  |
| aligned   | aligned   | aligned   |

This is rendered as follows.

column 1 column 2 column 3
this text this text this text
is left is centre is right
aligned aligned aligned

Online tools are available for generating Markdown syntax, for example https://www.tablesgenerator.com/markdown_tables.

16.1.2 Using knitr::kable()

The knitr package (Xie 2020), (Xie 2014), (Xie 2015) has the kable() function which can produce a table from a data frame. Here’s an example.

knitr::kable(head(mtcars))

which is rendered (in html) as follows:

mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1

16.2 Caching results for slow-running code

If some of your code takes a long time to run, it is preferable not to re-run it every time you edit your R Markdown document and want to re-compile it. It you use the code chunk option cache = TRUE, R will store (‘cache’) the results, and will only re-run the code chunk if it detects that something has changed.

Be careful if you use caching. For example, if you import a file and cache the results, R will not detect if the file contents change. When you knit your final version of your document, switch off all caching.

16.3 R Markdown for LaTeX users

The Markdown language is broadly the same as LaTeX for typesetting equations/maths notation, and so you can use LaTeX as normal for typesetting maths.

You can use any other LaTeX commands throughout your document. When producing pdf output, R Markdown will actually generate an intermediate .tex file, which is then compiled to pdf. However, these commands will be ignored if you knit to html or other formats.

Similarly, you can include html commands in your document; these will only work if you knit to html.

Try to keep to Markdown commands as far as possible. This will give you the most flexibility regarding your choice of output format (and you may wish to switch your output format in the future.)

For example, putting this in your document:

\section{Introduction}

will only produce the desired section heading if you knit to pdf, but using this:

# Introduction

will give the desired section heading for any output format you produce.

16.4 The YAML header

The first part of an R Markdown document, the ‘header’ is used to specify information such as author and title, and can include extra commands to change the appearance of the document. These are commands are specified using another language called YAML. Here’s an example of a YAML header, with a few extra commands specified that you might find helpful.

---
title: "My Report Title"
author: "Jeremy Oakley"
date: "2024-01-27"
output:
  pdf_document: 
    number_sections: true
  html_document: 
    number_sections: true
fontsize: 11pt
urlcolor: blue
header-includes:
  - \usepackage{bm}
---

Indenting of particular lines does matter in YAML. Keep this is mind when copying YAML examples.

The command Sys.Date() is an R command which will insert today’s date (year-month-day format). The commands fontsize and urlcolor affect pdf output only. The final command

header-includes:
  - \usepackage{bm}

can be used to add commands (e.g. \usepackage{bm}) to the preamble in the LaTeX document for pdf output.

References

Xie, Yihui. 2014. “Knitr: A Comprehensive Tool for Reproducible Research in R.” In Implementing Reproducible Computational Research, edited by Victoria Stodden, Friedrich Leisch, and Roger D. Peng. Chapman; Hall/CRC. http://www.crcpress.com/product/isbn/9781466561595.
———. 2015. Dynamic Documents with R and Knitr. 2nd ed. Boca Raton, Florida: Chapman; Hall/CRC. https://yihui.org/knitr/.
———. 2020. Knitr: A General-Purpose Package for Dynamic Report Generation in r. https://CRAN.R-project.org/package=knitr.