Introduction
An .Rmd file is an R Markdown
document: Markdown text with chunks of R code in it.
Rendering the document runs the code and puts its results, such as tables and charts,
into the output, which can be a web page, a PDF, a Word document or a slide show. It is
widely used for reports and teaching in statistics and data analysis.
Unlike a Jupyter notebook, the file holds only the source; the results are made again each time it is rendered.
Structure
An R Markdown file is plain text. It usually starts with a YAML header between two
lines of ---, giving the title, author and output format. The body is
Markdown. Code chunks are fenced with three backticks, with the language and options in
braces on the opening line, such as {r summary, echo=FALSE}. Short pieces
of code can also run inline, written as `r expression`.
Rendering is done by the rmarkdown R package: knitr runs the chunks and
writes a plain Markdown file with the results, and Pandoc turns that into the final
format. The header keys, chunk syntax and every chunk option are on the
header and chunks page.
---
title: "Fuel economy"
output: html_document
---
Cars in the `mtcars` data set average `r round(mean(mtcars$mpg), 1)` miles per gallon.
```{r plot, echo=FALSE}
plot(mtcars$wt, mtcars$mpg)
```
Not described
Known, not yet covered
- Reference: the options of each output format (
html_documentand the others); the chunks page names the formats only. They are in the R Markdown documentation. - Viewer: there is no viewer for R Markdown yet.
Links
Software
- RStudio – Posit’s IDE for R, which edits and knits R Markdown (Windows, macOS, Linux)
- rmarkdown – the R package that renders R Markdown (Windows, macOS, Linux)
- Quarto – publishing system that can also render
.Rmdfiles (Windows, macOS, Linux) - Pandoc – document converter that makes the final output (Windows, macOS, Linux)
- Visual Studio Code – code editor that edits them with an R extension (Windows, macOS, Linux)
Developers
- knitr – package that runs the code chunks and weaves in the results (R)
Reference
- R Markdown: The Definitive Guide – book by the package’s authors