M7 - Crafting Reports (REVISED)
R Markdown is a powerful tool for creating dynamic documents that combine code, text, and graphics. With R Markdown, you can write reproducible reports, scientific papers, and even books that seamlessly integrate R code, text, and graphics. In this lesson, we’ll focus on creating PDF documents using R Markdown, Markdown, YAML, and knitr. We’ll cover how to add a table of contents, figure captions, footnotes, and control over figure placement and alignment.
Getting Started
To begin, create a new Rmd document in RStudio by going to File > New File > R Markdown. Give your document a name and choose PDF as the output format. You can also specify the title, author, and date of your document in the YAML header.
---
title: "My Professional PDF"
author: "John Doe"
date: "February 23, 2023"
output:
pdf_document
---
In the YAML header, we specified the output format to be pdf_document
and set the toc
parameter to true
to include a table of contents in the output.
Adding a Table of Contents
To add a table of contents to our PDF, we need to modify the YAML header in our R Markdown file. YAML stands for “YAML Ain’t Markup Language” and is used to specify metadata about our document, such as the title, author, and output format. Here’s an example YAML header:
---
title: "My Professional PDF"
author: "John Doe"
date: "February 23, 2023"
output:
pdf_document:
toc: true
---
Adding Figure Captions
To add captions to our figures, we can use the fig.cap
parameter in our R code chunks. Here’s an example code chunk:
```{r pressure, echo=FALSE, fig.cap="This is a plot of pressure vs. temperature."}
plot(pressure, temperature)
```
In this code chunk, we set the fig.cap
parameter to “This is a plot of pressure vs. temperature.” This caption will be displayed underneath the figure in our PDF.
Adding Footnotes
To add footnotes, we can use the standard Markdown syntax. Here’s an example footnote:
Here is some text with a footnote[^1].
[^1]: This is a footnote.
In this example, we use the [^1]
syntax to create a footnote reference, and then add the footnote text at the bottom of the document.
Controlling Figure Placement and Alignment
To control where our figures are placed in the PDF, we can use the fig.pos
parameter in our R code chunks. Here are the possible values for fig.pos
:
"h"
: Place the figure “here”, i.e. at the exact position in the text where it appears in the code."t"
: Place the figure at the “top” of the page."b"
: Place the figure at the “bottom” of the page."p"
: Place the figure on a separate “page” that contains only figures and tables.
Here’s an example code chunk that places the figure at the top of the page and aligns it to the center:
```{r pressure, echo=FALSE, fig.pos
Step 2: Add content to your document
Once you’ve set up your Rmd document, you can start adding content to it. You can use Markdown syntax to format your text, add headers, create lists, and much more. Here’s an example of what your Rmd document might look like with some sample content:
# Introduction
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce ac tellus at nisi eleifend ultrices. Sed venenatis aliquam leo, eu tincidunt nisi eleifend sit amet. Proin et varius purus. Nullam bibendum, ante sed posuere rhoncus, lectus mi bibendum orci, sit amet interdum ex arcu et nisi.
## Methods
Sed dictum, arcu id venenatis ullamcorper, arcu mauris sodales velit, a consectetur velit felis in sapien. Suspendisse potenti. Maecenas pharetra magna sit amet urna maximus, quis volutpat lorem vehicula.
```{r, echo=FALSE}
library(ggplot2)
data <- data.frame(x = c(1, 2, 3), y = c(2, 4, 6))
ggplot(data, aes(x, y)) +
geom_point()
Results
Donec euismod dapibus turpis, vel tempor nulla vehicula ut. Nullam ut tristique massa. Morbi tincidunt ullamcorper nisi, at feugiat velit. Nullam malesuada semper nunc, in pretium metus bibendum id. Nulla facilisi.
vbnet
In this example, we've added headers, a code chunk that loads the `ggplot2` library and creates a simple scatterplot, and an image with a caption.
## Step 3: Customize the appearance of your PDF
To customize the appearance of your PDF, you can use LaTeX commands in your Rmd document. For example, you can change the font size or style, add a background color to your headings, or adjust the spacing between paragraphs. Here's an example of how you can use LaTeX commands to customize the appearance of your headings:
\textcolor{blue}{Introduction}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce ac tellus at nisi eleifend ultrices. Sed venenatis aliquam leo, eu tincidunt nisi eleifend sit amet. Proin et varius purus