What is R Markdown?

Overview

R Markdown (.Rmd), like plain Markdown (.md), is a markup language that allows one to generate formatted documents that can easily be converted to a structurally valid HTML file. However, R Markdown has some additional features which make it popular amongst data scientists and analysts. It is the perfect authoring framework for data science, as it allows us to perform the following tasks:

  • Save and execute code in R, Python, and SQL.
  • Generate plots.
  • Render output in numerous file formats.

Installation

R Markdown is free and open source. We can install the rmarkdown package using the following command in the R console:

install.packages("rmarkdown")
Installation command

How it works

An R Markdown file uses the .Rmd file extension. It can contain three types of content:

  • YAML header: It starts and ends with --- and contains the metadata of the document. For example, the title, author, tags, categories, and more. This is optional.
  • Code chunks: These are surrounded by ```. If these code chunks contain executable code, upon rendering, the output of the code is also displayed.
  • Text: This can be plain or formatted text using lists and font styling options.

Example

Let's take a look at how the R Markdown file renders as an HTML:

---
title: "Hello R Markdown"
author: "Educative"
categories: ["R"]
tags: ["R Markdown"]
---

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

# R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents.

We can also write in-line code like this:  
`number <- 1`  
We can embed an executable R code chunk like this:
``` {r}
x <- 3 + 3
print(x)
```


# Including Plots

We can also embed plots. See Figure \@ref(fig:pie) for example:

```{r pie, fig.cap='A fancy pie chart.', tidy=FALSE}
par(mar = c(0, 1, 0, 1))
pie(
  c(280, 60, 20),
  c('Sky', 'Sunny side of pyramid', 'Shady side of pyramid'),
  col = c('#0292D8', '#F7EA39', '#C4B632'),
  init.angle = -50, border = NA
)
```

Let's make a monthly active users plot!  
```{r barplot, fig.cap='Active users chart', tidy=FALSE}
barplot(
  c(35, 40, 25, 29, 10),
  main="Active users chart",
  names.arg=c("Jan", "Feb", "Mar", "Apr", "May"),
  xlab="Month",
  ylab="Active Users",
  col="#711A75"
)
```
R Markdown

Explanation

  • Lines 1–6: We add the document metadata between the --- markers.
  • Lines 19–22: We add an executable code chunk that displays when rendered.
  • Lines 29–37: We add code for plotting graphs in R Markdown. These graphs are displayed when rendered.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved