2024

Overview of R Markdown

  • Combines content and code
  • Outputs to multiple formats (e.g., HTML, PDF)

Setting Up Your Environment

Before starting, make sure you have:

  • Installed R and RStudio (RStudio is optional but recommended)

  • Installed the rmarkdown package using install.packages("rmarkdown")

  • A LaTeX distribution installed, like TinyTeX, for PDF output

Installing a LaTeX Distribution

  • To generate PDF documents from R Markdown, you need a LaTeX distribution.

  • TinyTeX is a minimal LaTeX distribution specifically designed for R Markdown.

Why TinyTeX?

  • Lightweight: Occupies less space and installs only necessary packages.

  • Easy to Install: Simplified installation process.

  • Automatic Package Installation: Automatically installs missing LaTeX packages when compiling R Markdown documents.

Step-by-Step Installation of TinyTeX

  1. Install the tinytex R Package: Run the following command in your R console:
install.packages("tinytex")
  1. Install TinyTeX: After installing the tinytex package, install TinyTeX using:
tinytex::install_tinytex()
  • This command downloads and installs the TinyTeX distribution.

  • TinyTeX integrates seamlessly with R Markdown and automatically installs missing LaTeX packages.

Creating Your First Rmd Document

  • Option 1: File -> New File -> R Markdown

  • Option 2: Simply create a new .Rmd file using your favourite plain text editor

Example R Markdown Document


---
title: "Minimal R Markdown Demo"
author: "Your Name"
date: "2024-03-05"
output: html_document
---

- This is a minimal demo of an R Markdown document.

# Heading 1

- The hashtags (`#`) denote section headings.

## Heading 2 

- The number of hashtags denotes the level of the
  heading.

### Heading 3

- Below is an R code chunk that generates a figure.


```r
library(data.table)
library(ggplot2)

# Create a plot
ggplot(pressure, aes(x = temperature, y = pressure)) +
  geom_point() +
  labs(title = "Pressure vs. Temperature",
       x = "Temperature (C)",
       y = "Pressure (mmHg)")
```

YAML Header

  • The YAML header specifies the document’s metadata (title, author, date) and output format (html_document).

  • This is a YAML header.

---
title: "Minimal R Markdown Demo"
author: "Your Name"
date: "`r Sys.Date()`"
output: html_document
---

R Code Chunks

  • R code chunks are denoted by three backticks and the letter r in curly braces.
```{r echo=T, eval=F}
library(data.table)
library(ggplot2)

# Create a plot
ggplot(pressure, aes(x = temperature, y = pressure)) +
  geom_point() +
  labs(title = "Pressure vs. Temperature",
       x = "Temperature (C)",
       y = "Pressure (mmHg)")
```

R Code Chunks

  • The echo argument controls whether the code is displayed in the output.

  • The eval argument controls whether the code is executed.

  • There are many other options but these should be all you need.

Compiling the Document

  • Option 1: To compile the document, click the “Knit” button in RStudio.

  • Option 2: Alternatively, use the render function from the rmarkdown package.

rmarkdown::render("your_file_name.Rmd")