Learning Objectives

  • Extend your RMarkdown toolkit with
  • Code chunk options
  • Links
  • Images
  • Themes
  • Tables

Chunk Options

In addition to giving your code chunks names, can use chunk options to further customize the behavior of your code chunks in RMarkdown.

Evaluating and hiding code and results

Take a moment to read the description of code chunks on this page.

Pay special attention to the code chunk options eval, echo, and results.

Figure Options

You can also use code chunks to control the appearance of your plots. The three most important chunk options are:

  • fig.width: the height of the output figure
  • fig.height: the width of the output figure
  • fig.asp: the aspect ratio of the output figure.
  • fig.align: the alignment of the figure. May be ‘center’, ‘right’, or ‘left’.

Figure size

As an example, let’s plot a histogram of the penguin flipper lengths:

hist(
  penguins$flipper_length_mm,
  xlab = "Flipper Length (mm)",
  main = "Histogram of Penguin Flipper Length")

Big histogram

To create this oversized plot, I’ve used the chunk options fig.width = 10 and fig.height = 12:

Wide histogram

You can think of the aspect ratio as the ratio of the height of the plot to its width. For example, an aspect ratio of 1/2 will produce a plot that is twice as wide as it is tall.

Here, I’ve used manually set the aspect ratio to 1/2 with the chunk option fig.asp = 0.5.

How could you modify the chunk option to make the plot twice as high as it is wide?

Tabsets

One way to organize contents in your RMarkdown html document is to use tabsets. I use the extensively to organize materials on the course website and assignment pages.

Tabsets are related to the concept of headers in RMarkdown. The R Markdown Cheetsheet shows how to use the number symbol, #, to create different level headers.

To initiate a tabset, you must first create a new header. The name of the header is then followed with the text {.tabset}. Every sub-header that follows will be in a separate tab. To terminate the tabset, simply create another header of the same level as the first one.

It’s easier to see the pattern using an example.

The following markdown produces a tabset:


## Example Tabset {.tabset}

The following subheaders will each be displayed in a tab:

### Tab 1

This is the *first* tab.

### Tab 2

The second tab is **much better**.

### Tab 3

I have a scatterplot:

```{r scatterplot_example_tab}
plot(rnorm(10), rnorm(10))
```

### Tab 4

I have a histogram

```{r histogram_example_tab, fig.aspect = 1/2}
hist(rnorm(100))
```

Example Tabset

The following subheaders will each be displayed in a tab:

Tab 1

This is the first tab.

Tab 2

The second tab is much better.

Tab 3

I have a scatterplot:

plot(rnorm(10), rnorm(10))

Tab 4

I have a histogram

hist(rnorm(100))

Document Themes

You can customize your document’s look by using one of R Markdown’s built-in themes. You can set the theme in the document’s YAML header. For example, I like to use the readable theme for many of the pages for this class:

---
title: "mike's example page"
output:
html_document:
theme: readable
---

Check out this RMarkdown theme gallery for some examples.

Table of Contents

If you have a long document, a floating table of contents helps the user to navigate easily.

Check out this page for an example of how to add a floating table of contents.

Report

For this assignment, you’ll be creating a RMarkdown document using all of the new skills you’ve learned.

Your document needs to have the following first-level sections

Introduction

  • Create a first-level section called ‘Introduction’. Refer to the RMarkdown Cheetsheet if you don’t remember how to do this.
  • This section needs to contain two lists:
  1. A bulleted list with that contains at least three concepts that you have enjoyed learning about in this course.
  2. A numbered list of at least three concepts you have found difficult.

Figures

For this section, you’ll use code chunk options to modify the appearance of your code and figures. In this section, you need to create three second-level headers. Your second-level headers will be tabs (review above to read about how to create a tabset)

1. Figure

In this tab, you’ll create a code chunk that plots a figure. You can use any data and plot type you like. For example, you might make a histogram of penguin flipper lengths.

2. Figure Without Code

In this tab, you’ll create another figure, but this time you need to use a code chunk option to hide the code.

3. Wide Figure Without Code

In your last tab, you’ll create a third figure using code chunks to:

  • Hide the code
  • Set the figure aspect ratio to 1/2.