R Markdown

Introduction - Markdown

Markdown is one of the markup languages along with HTML, XML and TeX. A markup language is deemed as a syntax for annotating a document where formatting is distinguishable from the textual content. That can be done by tags, that can be named elements (<b>bold</b>) or by ASCII punctuation marks and other non-letter symbols.

Markdown is defined as a text-to-HTML conversion tool based on the formatting syntax and the software tool to convert text to HTML. It was created in 2004 by John Gruber and Aaron Swartz and is classified a lightweight markup language. Its main advantage is having a comprehensive syntax, meaning that the plain text document is easily understandable, facilitating reading and writing.

# Heading with markdown
## Heading 2 with markdown

<h1>Header with HTML</h1> 
<h2>Heading 2 with HTML</h2>

It can be used to create different types of documents: books, presentations, websites, technical documentation and can be mixed with some other applications on R. Markdown is used in GitHub and Bitbucket software tutorials and Reddit posts. In fact, this training was organized on Markdown, combined with R, HTML and CSS. Markdown can also directly “understand” other languages as HTML, CSS and LaTeX. Due to this support, you can use HTML code directly on Markdown and it will be properly formatted.

The common filename extensions for Markdown documents are .md and .markdown. For R Markdown plain-text file the extension used is .Rmd. Jupyter notebooks text cells use Markdown.

Why use R Markdown?

R users can benefit from associating their codes with Markdown. It definitely helps in organizing the code by combining text, personal notes and scripts in the same document. This is a powerful approach for creating a reproducible workflow, which is essential in Science.

You can run your commands normally by using the code chunks. In addition, it helps in creating final reports that can be shared with your supervisor and colleagues.

Installation - R Markdown

R Markdown documents can be created directly on R (R Core team), you will just need to install Pandoc, that is a document converter - essential for mixing different types of markup languages in the same document. If you use RStudio, Pandoc comes with the IDE.

The next step is to install the rmarkdown package:

# From CRAN
install.packages("rmarkdown")

For generating pdf documents, you will also need to install LaTeX and its dependencies in your computer. There second option is to install TinyTex (Xie 2021) - a portable and lightweight LaTeX distribution, that can be installed in multiple platforms:

install.packages('tinytex')
tinytex::install_tinytex()  # install TinyTeX

LaTeX

We had problems to generate the pdf output without installing LaTeX. To install it, go to the LaTeX website and install any of the distributions available for your operating system (i.e., MacTex for Mac OS, TeX Live for Linux, and MiKTeX for Windows).

First R Markdown document

On the upper left corner, click in the New File button and select R Markdown (File -> New File -> R Markdown). Then choose a title for your document, include your name as author and select PDF as output format:

As you noticed, RStudio created a template document for you. Before we move forward, please save your file in the folder of your preference.

The beginning of the .Rmd contains metadata of the document between three dashes. It is formatted in YAML instead of Markdown. Do not worry about that, is just a data-serialization language that is important for the configuration files. It looks like:

---
title: "Research Design - R Markdown training"
author: "Author"
date: "11/29/2021"
output: pdf_document
---

See that it has the metadata of the file (title, author and date) as well as the output information (PDF format). Other information can be added, especially in the output part.

Please delete anything below the metadata in your Rmd document! We are going to build it together.

Text context

The body of the document, in Markdown syntax, can be organized after the metadata.

---
title: "Research Design - R Markdown training"
author: "Author"
date: "11/16/2021"
output: pdf_document
---

This is my first R Markdown document.

Now that we have the metadata and the content in the document, we can knit it!!! Briefly, that is how rmarkdown compiles the document to the chosen output (PDF, in our case).

Please, press the Knit button in the toolbar or press Ctrl/Cmd + Shift + K. RStudio will automatically open the output in a window.

Basic syntax

The syntax used in R Markdown is the Pandoc’s Markdown. Here we will describe the most common syntax elements for beginners.

Inline

Refers to formatting of strings that are in the the paragraph: italic, bold, subscript, superscript, hyperlinks, images, footnotes and code. We are going to put the string between special characters.

For italics and bold, we can use asterisks (*) or underscores (_):

*italics* or _italics_
**bold** or __bold__

Subscript and superscripts by tildes (~) and carets (^):

C~6~H~12~O~6~ + 6O~2~ --> 6CO~2~ + 6H~2~O

Fe^2+^ --> Fe^3+^ + e^-^

For hyperlinks, we use the syntax [text](link) where we can associate a link to a phrase or word.

Here is the link to access the [Montana State University](https://www.montana.edu) website.

Images have a similar syntax: ![text, image title or argument](path to image). Please, download the MSU logo here and save in the same location as your Rmd file. Then open type in your document:

![](./MSU-vert.png)

You can also use the website link:

![](https://www.montana.edu/creativeservices/images/MSU-vert.png)

Another option is to have the output of an R code as figure. We will do this further!

Footnotes can be included by a combination of a caret and square brackets ^[footnote].

To mark a text as inline code, just use backticks:

Write an inline `code`

Headers

Headers are important to create sections and subsections in your documents, organizing the topics you are working. They can be created up to six levels using the number sign #.

# Header - Level 1

## Header - Level 2

### Header - Level 3

#### Header - Level 4

Lists

In Markdown, there are different types of items lists.

Bullets

A bullet list of items begins with the symbols *, + or -:

* Item 1
* Item 2
* Item 3

We can also create nested lists, just by using more spaces:

* ANOVA
* Designs
   * Completely Randomized Design
   * Randomized Block Design

Ordered

Ordered lists start with enumerators like 1. item or 1) item:

1. Hey

2. Enumerated

3. List

   * unordered list

Checklist

We can create a checklists using [ ] and [X]:

- [ ] unchecked task 
- [x] checked task
  • unchecked task
  • checked task

Math expressions

In R Markdown the mathematical expressions are written as in LaTeX. We just need to put the expression between dollar signs $:

$y_{ij} = \beta_{0} + \beta_{i} + \gamma_{j} + (\beta\gamma)_{ij}$

Other math environments can be used:

$\begin{bmatrix} y_{11} \\
y_{12} \\
y_{21} \\
y_{22}
\end{bmatrix} = 
\begin{bmatrix} 1 & x_{11} \\
1 & x_{12} \\
1 & x_{21} \\
1 & x_{22}
\end{bmatrix}$

R code chunks

Now it is time to put together everything you have been learning about Research Design and R! To create a code chunk, just click on the Insert a new code chunk or by the shortcut (Ctrl + Alt + I or Cmd + Option + I):

```{r}


```

Inside the chunk we can write our R code, as we do in a script. To print the simple Hello World, we just include it in the chunk:

```{r}
cat("Hello World")
```
## Hello World

Now, let’s use the content from Unplanned Comparisons to illustrate how to include an analysis inside R Markdown.

```{r}
# This script illustrates the use of means separations tests
# read in and inspect the data
# Make data frame
weight <- c(4.23, 4.38, 4.10, 3.99, 4.25,
            3.85, 3.78, 3.91, 3.94, 3.86,
            3.75, 3.65, 3.82, 3.69, 3.73,
            3.66, 3.67, 3.62, 3.54, 3.71)
acid <- c(rep("Control", 5),
          rep("HCl", 5), 
          rep("Propionic", 5),
          rep("Butyric", 5))

d <- data.frame(acid, weight)
d$acid <- as.factor(d$acid)
str(d)
```

If we knit the document, we will get the following output:

## 'data.frame':    20 obs. of  2 variables:
##  $ acid  : Factor w/ 4 levels "Butyric","Control",..: 2 2 2 2 2 3 3 3 3 3 ...
##  $ weight: num  4.23 4.38 4.1 3.99 4.25 3.85 3.78 3.91 3.94 3.86 ...

This is not limited to text output. Plots can be directly shown on document:

```{r}
boxplot(weight ~ acid, data = d)
```

Now you can organize your results from ANOVA and any other tests in a single report. We can also make things fancier. First, let’s create an ANOVA table based on the linear model:

```{r}
mod1<-lm(weight ~ acid, data = d)
anova(mod1)
```
## Analysis of Variance Table
## 
## Response: weight
##           Df  Sum Sq  Mean Sq F value    Pr(>F)    
## acid       3 0.87369 0.291232  33.874 3.669e-07 ***
## Residuals 16 0.13756 0.008597                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

We can then use the function kable from package knitr:

```{r}
mod1<-lm(weight ~ acid, data = d)
knitr::kable(anova(mod1))
```
Df Sum Sq Mean Sq F value Pr(>F)
acid 3 0.873695 0.2912317 33.87399 4e-07
Residuals 16 0.137560 0.0085975 NA NA

Controlling the outputs

We can control the outputs of the chunks using arguments inside the curling brackets ```{r OPTION}. Among the options, we can control the width and height of the figure, align the plot, evaluate the chunk and also hide it.

Evaluate the code

```{r eval=FALSE}
cat("I will not be evaluated!")
```

Hide the code

```{r echo=FALSE}
cat("My code is hidden! :) ")
```

Hide the result

```{r result='hide'}
cat("No result is shown here!")
```

Warnings, messages and errors

We can also hide messages provided by the packages, as well as warnings and errors. We simply use warning=FALSE.

```{r warning=FALSE}
warning("This is a warning")
```

Figure parameters

Width, heigth and alignment:

```{r fig.width=5, fig.height=3, fig.align='center'}
boxplot(weight ~ acid, data = d)
```

Figure caption:

```{r fig.width=5, fig.height=3, fig.cap="Figure 1 - This is our first figure."}
boxplot(weight ~ acid, data = d)
```
Figure 1 - This is our first figure.

Figure 1 - This is our first figure.

Other languages

The package knitr offers support to a large number of language engines. It allow us to create documents using other programming languages (combined in the same document or not).

Bash

```{bash}
echo "Hello world"
```
## Hello world

Python

```{python, python.reticulate=FALSE}
x = 'hello world!'
print(x)
print(x.split(" "))
```
## hello world!
## ['hello', 'world!']

Questions ?

References

Xie, Y.; Allaire, J.J.; Grolemund, G. R Markdown: The Definitive Guide

Daring Fireball - by John Gruber. Markdown

Contact

Lachowiec Lab
Office: 305 Plant Bioscience Building
Lab: 331 Plant Bioscience Building
Phone: 406-994-2903 (office)
406-994-4222 (lab)


Fernando Correr
fernando [dot] hcorrer [at] gmail [dot] com


Jennifer Lachowiec
jennifer [dot] lachowiec [at] montana [dot] edu