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.
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)
```
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:
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)
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)
```
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:
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:
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:
There are lots of good resources for learning about code chunks and options.
A couple of places to start are:
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:
Here’s some text
Here’s some graphics:
Here’s a numbered list:
This section is not in a tab
Still no tabs!
This seciton uses a ‘pill style’ of tabs!
Here’s some text
Here’s some graphics:
Here’s a numbered list:
Things to note: