Skip to content

RMarkdown

RMarkdown is a powerful tool that combines the simplicity of Markdown with the analytical power of R. It allows you to create dynamic documents that include both your analysis and its results, making it perfect for scientific reports, lab notebooks, and reproducible research.

Getting Started with RMarkdown

To create a new RMarkdown document in RStudio:

  • Go to File > New File > R Markdown
  • Choose a title and author for your document
  • Select your preferred output format (HTML, PDF, or Word)

Basic Components

YAML Header

Every RMarkdown document starts with a YAML header that contains metadata about your document:

---
title: "My Analysis"
author: "Your Name"
date: "2024-03-19"
output: html_document
---

Text Formatting

RMarkdown uses simple syntax for text formatting:

# This is a heading
## This is a subheading

*This text is in italics*
**This text is bold**

- This is a bullet point
- This is another bullet point

1. This is a numbered list
2. This is another item

[This is a link](https://www.example.com)

Code Chunks

Code chunks are where you write and execute R code. They start with three backticks and {r}:

```{r}
# Your R code goes here
x <- 1:10
mean(x)
```

You can add options to code chunks to control their behavior:

```{r, echo=FALSE, warning=FALSE}
# This code will run but not show the code itself
# Warnings will be suppressed
```

Common Code Chunk Options

Code chunk options control how your code and its output are displayed in the final document. Here are the most commonly used options:

General Options

  • echo=FALSE: Hide the code but show the output
  • eval=FALSE: Show the code but don’t run it
  • include=FALSE: Run the code but don’t show code or output
  • warning=FALSE: Hide warnings
  • message=FALSE: Hide messages
  • error=FALSE: Hide error messages

Figure Options

  • fig.width= and fig.height=: Set figure dimensions in inches
  • fig.cap=: Add a figure caption
  • fig.align=: Align figures (“left”, “center”, or “right”)
  • out.width= and out.height=: Control the size of figures in the output document
  • dpi=: Set the resolution of the figure (dots per inch)

Example of using figure options:

```{r plot-example, fig.width=8, fig.height=6, fig.cap="My beautiful plot", fig.align="center"}
ggplot(data, aes(x = value)) +
  geom_histogram() +
  theme_minimal()
```

Inline Code

You can include R code directly in your text using single backticks (`) and the letter r followed by a space as the prefix:

The mean of our data is `r mean(x)`.

Knit Your Document

Knit is the process of converting your RMarkdown document into its final format. When you knit a document, R runs all the code chunks and combines the results with your text to create a complete document.

How to Knit

  • Click the “Knit” button in RStudio (looks like a ball of yarn)
  • Or use the keyboard shortcut: Ctrl+Shift+K (Windows/Linux) or Cmd+Shift+K (Mac)

Output Formats

RMarkdown supports multiple output formats. The most common ones are:

  • HTML: Creates a webpage that can be viewed in any browser. Great for interactive elements and sharing online.
  • PDF: Creates a PDF document. Requires LaTeX to be installed on your system. Best for formal reports and publications.
  • Word: Creates a Microsoft Word document. Useful when you need to share with collaborators who prefer Word.

You can specify the output format in your YAML header:

---
title: "My Analysis"
output:
  html_document:    # For HTML output
    toc: true      # Add a table of contents
    theme: cosmo   # Choose a theme
  pdf_document:     # For PDF output
    toc: true
    number_sections: true
  word_document:    # For Word output
    toc: true
---

Knit Options

You can customize the knitting process using various options in your YAML header:

  • toc: true: Add a table of contents
  • number_sections: true: Number the sections automatically
  • code_folding: hide: Hide code chunks by default
  • highlight: tango: Choose a syntax highlighting theme
---
title: "Simple Data Analysis"
author: "Your Name"
date: "2024-03-19"
output: html_document
---

# Introduction

This is a simple analysis of some ecological data.

## Data Analysis

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

```{r load-data}
# Load and examine the data
data <- read.csv("my_data.csv")
head(data)
```

## Results

The mean value in our dataset is `r mean(data$value)`.

```{r plot, fig.cap="Distribution of values"}
ggplot(data, aes(x = value)) +
  geom_histogram() +
  theme_minimal()
```

Further Resources