- Combines content and code
- Outputs to multiple formats (e.g., HTML, PDF)
2024
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
To generate PDF documents from R Markdown, you need a LaTeX distribution.
TinyTeX is a minimal LaTeX distribution specifically designed for R Markdown.
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.
tinytex
R Package: Run the following command in your R console:install.packages("tinytex")
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.
Option 1: File -> New File -> R Markdown
Option 2: Simply create a new .Rmd
file using your favourite plain text editor
---
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)")
```
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
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)")
```
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.
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")