- What is R Markdown / Bookdown?
- Basic demos of R Markdown and Bookdown, using RStudio Cloud
- Tips & tricks
Teaching Development Morning, 14 December 2021
These slides were written in R Markdown, using the ioslides
package. (More later.)
Visit https://mpaldridge.github.io/rmarkdown/ for:
R Markdown is a system for writing mathematical/statistical documents.
Bookdown is a package for R Markdown that provides extra features useful for long documents.
Markdown is a “mark-up” language (like LaTeX or HTML), where special characters and commands tell the computer what formatting you want.
Markdown:
# About MATH1710 ## Syllabus This module is **MATH1710 Probability and Statistics 1** and will last for *11 weeks*. We will cover three sections: 1. Exploratory data analysis 2. Probability - Probability with events - Probabiltiy with random variables 3. Bayesian statistics
LaTeX:
\chapter{About MATH1710} \section{Syllabus} This module is \textbf{MATH1710 Probability and Statistics 1} and will last for \emph{11 weeks}. We will cover three sections: \begin{enumerate} \item Exploratory data analysis \item Probability \begin{itemize} \item Probability with events \item Probabiltiy with random variables \end{itemize} \item Bayesian statistics \end{enumerate}
Markdown is a “mark-up” language (like LaTeX or HTML), where special characters and commands tell the computer what formatting you want.
R Markdown is a system to write and compile (“knit”) Markdown documents using R…
R is a programming language that is very good at doing statistics.
Using the program RStudio is a convenient way to work with the language R (and with R Markdown documents).
Demos later…
R Markdown is a system to write and compile (“knit”) Markdown documents using R.
R Markdown adds to plain-Markdown:
amsmath
extras: mathbb
, mathcal
, align
, align*
, cases
, pmatrix
, etcBookdown is an extra package for R Markdown that is particularly useful for long documents.
Compare earlier demos:
“Plain” R Markdown | R Markdown with Bookdown |
---|---|
Good for short documents | Good for long documents |
Slightly simpler to write | Slightly trickier to write |
PDF or accessible HTML | PDF or accessible HTML |
LaTeX equations | LaTeX equations |
Single HTML page | Multi-page website |
No theorem environments | Theorem environments |
I strongly recommend using R Markdown via RStudio.
To install on your own computer:
For a first try, I recommend instead the RStudio Cloud: https://rstudio.cloud
[demo via RStudio Cloud]
Appearance | (R) Markdown code |
---|---|
Bold text | **Bold text** |
Italic text | *Italic text* |
Fixed width |
`Fixed width` |
Link | [Link](https://eps.leeds.ac.uk/maths) |
# Chapter ## Section ### Subsection
Writing writing writing writing. - Leave a spare line - Dash then space for bulleted lists 1. Indent for sub-lists 1. Use number, dot, space for enumerated lists 1. Markdown will ensure consecutive numbering - The end.
Writing writing writing writing.
Or, use the visual editor.
Bookdown is trickier in RStudio Cloud, so I will revert to RStudio as installed on my computer.
In R Studio:
File
New Project
New Directory
Book project using bookdown
Create Project
LaTeX:
\begin{theorem}[Fermat's last theorem] \label{FLT} No three positive integers $a$, $b$, and $c$ satisfy the equation $a^n + b^n = c^n$ for any integer value of $n$ greater than $2$. \end{theorem} As we saw in Theorem \ref{FLT}\dots
R Markdown, with the Bookdown package:
::: {.theorem #FLT name="Fermat's last theorem"} No three positive integers $a$, $b$, and $c$ satisfy the equation $a^n + b^n = c^n$ for any integer value of $n$ greater than $2$. ::: As we saw in Theorem \@ref(thm:FLT)...
Previously,
```{theorem} Your theorem here. ```
had been recommended, but the newer
::: {.theorem} Your theorem here. :::
method is better.
Theorem-like environments in Bookdown are much less flexible than their LaTeX equivalents.
theorem
, lemma
, corollary
, proposition
, conjecture
, definition
, example
, exercise
, hypothesis
Here is some R code:
data <- rnorm(100) mean(data) hist(data)
R experts will know that this:
I can include that R code it in an R Markdown document like this:
Here is some R code: ```{r} data <- rnorm(100) mean(data) hist(data) ```
Note that the code is preceded with three “backticks” ```
and {r}
, then ends with three more backticks.
The result is the following…
Here is some R code:
data <- rnorm(100) mean(data)
## [1] -0.1800475
hist(data)
Note that the document displayed both the R commands and the output those commands produced.
It is apparently possible to use other programming languages in R Markdown in the same way I demonstrated R code.
```{python}
See Chapter 15 of the R Markdown cookbook for details.
I don’t know anyone local who’s tried this out and can advise us – audience?
Tables in (R) Markdown are a bit fiddly…
| "Plain" R Markdown | RMarkdown with Bookdown | |:--------------------------:|:--------------------------:| | Good for short documents | Good for long documents | | Slightly simpler to write | Slightly trickier to write | | PDF or accessible HTML | PDF or accessible HTML | | LaTeX equations | LaTeX equations | | Single HTML page | Multi-page website | | No theorem environments | Theorem environments |
… so I recommend this online Markdown table generator.
It is good accessibility practice to describe pictures with “alt text”, for those using screenreaders.
Until recently, R Markdown simply re-used the caption as alt text…
New syntax (as of April 2021):
```{r demo-pic, fig.cap="Histogram of data", fig.alt="Histogram of data, showing a bell-shaped symmetric curve around x = 0, dying away at x = -3 and x = +3"} knitr::include_graphics("/figures/hist.png") ```
fig.cap = "..."
gives the (short) captionfig.alt = "..."
gives the (long) alt textIf you use TikZ to draw pictures in LaTeX, you can do this in R Markdown too.
```{tikz demo-tikz, echo=FALSE, fig.cap="Caption", fig.alt="Alt text"} \begin{tikzpicture} \draw[thick] (0,0) rectangle (6,4); \end{tikzpicture} ```
Default picture types:
Many of us will want to knit Bookdown documents to both PDF and HTML.
preamble.tex
style.css
style.css
file that does these (and some other things)ClavertonDown is a package by the University of Bath, based on Bookdown.
It is arguably the gold standard in making accessible mathematical documents.
Phil will (I think) demonstrate this shortly.
R Markdown has a few formats for making slides.
ioslides
(it seems to me) which is what these slides useFile
— New File
— R Markdown
and choose “Presentation”I want R (or other) code executed within my document: R Markdown with optional Bookdown
I want a system with a large user base that is under active development: R Markdown with optional Bookdown, probably
I want very smart appearance of an HTML website with minimal effort: Bookdown
I want to convert existing LaTeX notes with minimal fiddling around: LaTeXML
I want to produce HTML notes while learning minimal new stuff: LaTeXML
The “bibles”:
My guide to R Markdown and Bookdown (a bit out of date)
Locals I know of with R Markdown and/or Bookdown experience: Luisa Cutillo, Serguei Komissarov, Richard Mann, Graham Murphy, Jochen Voss, Phil Walker, …
Visit https://mpaldridge.github.io/rmarkdown/ for: