1 Introduction

R Markdown is an extension of Markdown (https://daringfireball.net/) that is used inside of R by loading the rmarkdown package written by Allaire et al. (2017). R Markdown supports several static and dynamic (program code and narrative) output formats including:

For more information on R Markdown output formats please see http://rmarkdown.rstudio.com/. This article will focus on the benefits a high school mathematics instructor will realize from using R Markdown and the creation of documents one might use in a high school mathematics course. R (R Core Team 2017) and RStudio (RStudio Team 2016) and LaTeX are all open source software projects which means there is no cost to download and to install them on your personal machine.

2 Benefits of using R Markdown

An immediate benefit of using R Markdown is the easy creation of professional documents from simple text files. Since R Markdown uses text files, the user’s workflow is recordable and can be easily maintained with a form of version control such as Git (https://git-scm.com/) on a platform such as GitHub (https://github.com/) or Bitbucket (https://bitbucket.org/product), both of which offer unlimited free public repositories. The number of R Markdown commands (less than 20) one needs to control the appearance of a document and the ease of using the commands is a huge selling point. RStudio has a quick reference guide the user can view in the right pane of editor by clicking Help > Markdown Quick Reference (See Figure 2.1). RStudio also maintains a two page R Markdown Cheat Sheet at https://www.rstudio.com/wp-content/uploads/2016/03/rmarkdown-cheatsheet-2.0.pdf that discusses the YAML header, important code chunk options, and many other useful tidbits. For a mathematics instructor, one of the greatest advantages of using R Markdown is that mathematical symbols and equations can be inserted using standard LaTeX without having to spend an inordinate amount of time specifying a front matter as is typical when using LaTeX alone. R Markdown documents are dynamic, meaning program code and text narrative are in the same document so analysis/computing and writing stay together. The user may choose whether to display or hide the computer code by using code chunks with appropriate options.

Markdown Quick Reference help displayed in the right pane of the RStudio IDE

Figure 2.1: Markdown Quick Reference help displayed in the right pane of the RStudio IDE

3 A Minimal Example

The following source code is used to create Figures 3.1 and 3.2. The source code illustrates a LaTeX environment (align), several R code chunk options, R code to draw and compute the roots of a polynomial, and inline R code that extracts answers from R code and weaves the result into the text. The YAML written between lines of ---, has only one key:value pair title: "A Minimal Example". Inline mathematics is written between single dollar signs ($) and display mathematics is written between double dollar signs ($$). More complex LaTeX environments for HTML and PDF documents are specified in the same fashion as one would use when writing with LaTeX. There are many free online references for using LaTeX such as https://en.wikibooks.org/wiki/LaTeX/Mathematics. Equations, theorems, figures, and tables can all be cross-referenced using the bookdown package by Xie (2017), which is built on top of R Markdown. While the source code for A Minimal Example does not use numbered equations or figures, the interested reader can refer to the documentation at https://bookdown.org/yihui/bookdown/ for examples on how to number and cross-reference equations, theorems, figures, and tables.

---
title: "A Minimal Example"
---

Solve $6x^2=3x$ by factoring.

\begin{align*}
6x^2 &= 3x\\
6x^2 - 3x &= 0\\
3x(2x - 1) &=0\\
3x & =0 \Longrightarrow x = 0\\
2x - 1&= 0 \Longrightarrow x = \tfrac{1}{2}\\
\end{align*}

```{r, fig.align = "center", fig.width = 4, fig.height = 4, comment = NA}
f <- function(x){6*x^2 - 3*x}          # define function f
curve(f, from = - 0.25, to = 0.75)     # graph function f
abline(a = 0, b = 0, lty = "dashed")   # add dashed horizontal line
polyroot(c(0, -3, 6))                  # finding the roots with polyroot()
Re(polyroot(c(0, -3, 6)))              # Real roots
```

The real roots of $6x^2=3x$ are `r Re(polyroot(c(0, -3, 6))[1])` and `r Re(polyroot(c(0, -3, 6))[2])`. 
HTML output

Figure 3.1: HTML output

PDF output (LaTeX)

Figure 3.2: PDF output (LaTeX)

Source code is written the top left pane of the RStudio IDE (See Figure 3.3). To start a new R Markdown file, click File > New File > R Markdown…. To compile the document, click the Knit Button.

RStudio Integrated Development Environment

Figure 3.3: RStudio Integrated Development Environment

4 Downloading and Installing R, RStudio, and LaTeX

R, RStudio, and LaTeX run on a wide variety of UNIX platforms, Windows, and MacOS. To download R, point your browser to https://cloud.r-project.org/, and select one of the precompiled binary distributions that matches your computer’s operating system. To download RStudio, point your browser to https://www.rstudio.com/products/rstudio/download/, and select the installer that matches your computer’s operating system. Install both the downloaded files. For the more adventurous user, it is possible to create your own RStudio server by following the directions Dean Attali has posted at http://deanattali.com/2015/05/09/setup-rstudio-shiny-server-digital-ocean/. TeX live (http://www.tug.org/texlive/) is a major TeX distribution for Linux, Windows, and MacOS; however, if you are using Windows, many users prefer MikTeX (https://miktex.org/) and if you are using MacOS most users install MacTeX (http://www.tug.org/mactex/). Follow the directions provided on the operating system’s specific web sites for installing LaTeX. The author recommends performing a complete LaTeX installation.

5 Additional Resources

If one is new to R, DataCamp (https://www.datacamp.com/) offers both free and premium content courses on a wide variety of R topics. One can find RStudio maintained Cheat Sheets for many topics as well as user contributed Cheat Sheets at https://www.rstudio.com/resources/cheatsheets/. The article Using R with the RStudio Integrated Development Editor - Ideas, Pointers, and Teasers may provide the reader with additonal resources and ideas for using R with RStudio.

References

Allaire, JJ, Yihui Xie, Jonathan McPherson, Javier Luraschi, Kevin Ushey, Aron Atkins, Hadley Wickham, Joe Cheng, and Winston Chang. 2017. Rmarkdown: Dynamic Documents for R. https://CRAN.R-project.org/package=rmarkdown.

R Core Team. 2017. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing. https://www.R-project.org/.

RStudio Team. 2016. RStudio: Integrated Development Environment for R. Boston, MA: RStudio, Inc. http://www.rstudio.com/.

Xie, Yihui. 2017. Bookdown: Authoring Books and Technical Documents with R Markdown. https://CRAN.R-project.org/package=bookdown.