Dear students,
This week we will learn how to use rmarkdown
to produce documents from within RStudio
. This means that we have just a single file that generates, publishes and type-sets all our outcomes into a single target document.
I’ll quickly walk you through the basics, but I strongly suggest that you follow this elaborate quick course afterwards.
Let’s start.
Gerko
rmarkdown
rmarkdown
is RStudio's
document interface that, just like markdown
, provides users with a lightweight markup language with plain text formatting syntax. This document, for example, is prepared using rmarkdown
. Have a look at this (Exercise 5.html
) and Exercise 5.Rmd
files and you’ll see that the body of the document is just text (somewhat like with \(\LaTeX\) - but simpler) and that figures, links, code, tables - and so on - are easily marked up by simple commands or straightforward use of text symbols. Let’s have a look at some of these components.
Every rmarkdown
document starts with a header. In this header, the output format is specified, as well as information regarding the title and authorship of the document. Whenever a new rmarkdown
document is opened in RStudio
, the following default header appears for a html document:
---
title: "Untitled"
author: "YOURNAME"
date: "TODAYSDATE"
output: html_document
---
The header can be expanded to contain document-wide display and redering parameters.
The beauty in rmarkdown
is to weave text and code into a single document. There are many languages that plug into rmarkdown
, but for now we focus on R
. To run R
code, a code chunk has to be formally declared:
```{r}
a <- 100
```
The above code renders a code chunk, which will be printed by default. There are many options that can be explored with respect to code chunks, such as caching, evaluation and printing, For example, the following code renders a chunk that is evaluated and cached, but not printed (= echoed) to the final document:
```{r eval = TRUE, echo = FALSE, cache = TRUE}
a <- 100
```
We use
`this`
to render in-line code as this
. Consequently, we can use code executions to obtain in-line evaluations of previously rendered r-code via
`r a*3`
to evaluate that \(a \times 3\) equals 300.
Use *
to put emphasis on (parts) of text. For example
*This text will be in italic*
renders This text will be in italic**This text will be in bold face**
renders This text will be in bold face***This text will be in both bold face and italic***
renders This text will be in both bold face and italicRaw \(\LaTeX\) can be used via $X \sim \mathcal{N}(\mu,\,\sigma^{2})$
to render \(X \sim \mathcal{N}(\mu,\,\sigma^{2})\) as in-line equation. However, the same can be obtained as displayed equation via $$X \sim \mathcal{N}(\mu,\,\sigma^{2})$$
to render
\[X \sim \mathcal{N}(\mu,\,\sigma^{2})\]
Sections are created with symbol #
, where
# The First
conforms to \section{The First}
in \(\LaTeX\),## Deeper level
conforms to \subsection{Deeper level}
,### Even deeper level
conforms to \subsubsection{Even deeper level}
,Urls are displayed via [the text](the url)
such that [Gerko Vink](https://www.gerkovink.com)
is rendered as Gerko Vink.
To post externally generated figures in rmarkdown
, use 
to generate the following figure (source)
and use 
to generate the same figure with a caption.
This is a caption
For centering and resizing graphs, I find it most convenient to post the figure in a html container:
<center> <img src="figure.jpg" width = 50%> </center>
Note that the above figure is now centered and spans 50%
of the width of the document.
Creating tables in markdown
or rmarkdown
is very straightforward. For example, the following code
First Header | Second Header
---------------- | -------------
Content Cell 1,1 | Content Cell 1,2
Content Cell 2,1 | Content Cell 2,2
generates the following table:
First Header | Second Header |
---|---|
Content Cell 1,1 | Content Cell 1,2 |
Content Cell 2,1 | Content Cell 2,2 |
Design a study that:
When finished, push to your GitHub fork of markup2020
and create a pull request
.