Introduction

The true power of RMarkdown is that you can include R code, graphics, and regular text into a single report or webpage.

In fact, you’ll be using RMarkdown to make your final project for this course.

This demo will allow you to practice creating code chunks and using code chunk options.

Code Chunks

In your RMarkdown assignment, you had a chance to practice creating an RMarkdown document that included headers and lists. In this demo, you’ll learn to create code chunks.

Let’s practice now! Start by creating a new RMarkdown document and deleting everything below the setup code chunk:

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

Inserting Code Chunks

If you look above at info bar just above this window, you’ll see a little green icon that has a plus sign and the letter C.

Hover over the button now and you’ll see that it says “Insert a new code chunk”.

Click the button and select R to insert an R code chunk.

You should see something that looks like:

```{r}

```

A few things to note:

  • The ‘backtick’ or ‘accent grave’ characters at the top and bottom of the chunk: All the text that you place in this section will be interpreted as R code.
  • The curly braces with the letter r: This indicates to RStudio that the section between the sets of backticks contains R code.

Code Chunk Code

You’ve created a blank code chunk, now it’s time to add some code!

First of all, let’s load the palmerpenguins package and plot a histogram of body mass. Add the following code to the body of your code chunk and knit your document:


require(palmerpenguins)
hist(penguins$body_mass_g)

In your knitted document, you should see the R code you wrote as well as a histogram. Your document should have something like the following:

require(palmerpenguins)
## Loading required package: palmerpenguins
hist(penguins$body_mass_g)

Naming Your Chunk

It’s a good idea to name your code chunks. This makes it easier to debug your code.

To give your chunk a name, simply type a space after the r and type a name. For example, you might name your first chunk histogram_1 Your chunk would then look like:

```{r histogram_1}
require(palmerpenguins)
hist(penguins$body_mass_g)
```

Chunk Options

Hiding Code

Let’s say you wanted to plot the histogram, but you didn’t want the code itself to show up in the document.

To accomplish this, you’ll use a code chunk option. In this case, the ‘echo’ option.

```{r histogram_no_code, echo = FALSE}
require(palmerpenguins)
hist(penguins$body_mass_g)
```

The result should be something like:

Things to note:

  • I placed a comma between the chunk name and the chunk option.
  • The first entry after the r will always be the chunk name. Names do not use the equals symbol.
  • The chunk option listed the name of the option, followed by and equls symbol and the value of the option.
  • You cannot use duplicate chunk names
  • I named this chunk ‘histogram_no_code’.

Changing figure dimensions.

Next, let’s try plotting a multi-panel histogram.

Create a new chunk (call it ‘multi_panel_1’) and use the following code to make a 4-panel plot:

par(mfrow = c(2, 2))
hist(
penguins$body_mass_g,
main = "Histogram of Body Mass",
xlab = "Body Mass (g)")
hist(
penguins$flipper_length_mm,
main = "Histogram of Flipper Length",
xlab = "Flipper Length (mm)")
boxplot(
body_mass_g ~ species, data = penguins,
main = "Body Mass and Species",
ylab = "Body Mass (g)"
)
boxplot(
flipper_length_mm ~ species, data = penguins,
main = "Flipper Length and Species",
ylab = "Flipper Length (mm)"
)

Things to note:

  • Remember to paste the code into the body of a code chunk. RStudio won’t interpret text outside of a code chunk as R code.
  • That took a lot of code. You probably wouldn’t want to show all of it in a report.
  • The proportions of the plot make the individual panels difficult to see. How could we fix this? A taller plot, perhaps?

Figure Width and Height Options

We will now use code chunk options to set the dimensions of the plot manually. Here’s a skeleton including the relevant chunk options:


```{r multi_panel_2, echo = FALSE, fig.width = 7, fig.height = 10}
# Plotting code goes here
```

The result will look like this:

  • You now have a taller figure. You should tinker with the width and height options to get a feel for how they work.
  • I used the echo = FALSE option again to hide the code.

Chunk Resources and References

There are lots of good resources for learning about code chunks and options.

A couple of places to start are:

Tabsets

A convenient way to organize your content in long documents is to use tabsets.

I’ll demonstrate the concept with a quick example:

Copy the following, paste it into your RMarkdown document, and knit it.

Examine the result.


# A New Section With Tabs {.tabset}

## Tab 1

Here's some text

## Tab 2

Here's some graphics:

```{r tab_graph 1, echo = FALSE}
boxplot(
  flipper_length_mm ~ species, data = penguins,
  main = "Flipper Length and Species",
  ylab = "Flipper Length (mm)"
)

```

## Tab 3

Here's a numbered list:

1. Item 1
1. Item 2
1. Item 3
1. **Item 4** (in bold)

# New 1st Level header (not there are no tabs)


## Subsection 1

This section is not in a tab

## Subsection 2

Still no tabs!

# Tab Section 2 {.tabset .tabset-pills}

This seciton uses a 'pill style' of tabs!

## Tab 1

Here's some text

## Tab 2

Here's some graphics:

```{r tab_graph 1, echo = FALSE}
boxplot(
  flipper_length_mm ~ species, data = penguins,
  main = "Flipper Length and Species",
  ylab = "Flipper Length (mm)"
)

```

## Tab 3

Here's a numbered list:

1. Item 1
1. Item 2
1. Item 3
1. **Item 4** (in bold)

You should get something like:

A New Section With Tabs

Tab 1

Here’s some text

Tab 2

Here’s some graphics:

Tab 3

Here’s a numbered list:

  1. Item 1
  2. Item 2
  3. Item 3
  4. Item 4 (in bold)

New 1st Level header (without tabs)

Subsection 1

This section is not in a tab

Subsection 2

Still no tabs!

Tab Section 2

This seciton uses a ‘pill style’ of tabs!

Tab 1

Here’s some text

Tab 2

Here’s some graphics:

Tab 3

Here’s a numbered list:

  1. Item 1
  2. Item 2
  3. Item 3
  4. Item 4 (in bold)

Things to note:

  • The tabs apply to all the second-level header sections folowing the first-level header tabset declaration.
  • The following second-level headers were all in tabs.
  • To end the tabset, I created another first-level header.