B Introduction to Rmarkdown

B.1 Install Rmarkdown

Rmarkdown is a file format that helps you write reports which combine texts and codes and code outputs in an easy way. In fact, this book was written in Rmarkdown as well!

In order to work with Rmarkdown, you will need to install the rmarkdown package

install.packages('rmarkdown')

To write a new Rmarkdown file:

  • Click File > New File > R Markdown or

  • click the New File button under the File button in the top left of the RStudio window, choose R Markdown.

You can choose the output format that you want, but usually we will choose PDF or HTML format. To generate PDF output, you will need to install LaTeX. You can install an independent LaTeX distribution, for example, personally I use MiKTeX. However, for anyone who never uses LaTeX before, you can just install tinytex, which is a convenient, lightweight, and beginner-friendly LaTeX distribution

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

B.2 Components of A Rmarkdown File

When you open a new Rmarkdown file, you will see that the top of the file contains some information about the file, for example, title, date, output format, etc. This component is written in between two --- signs. This is called the metadata of the file. You can change the metadata by typing the name, date, author of your file.

Then, you can start writing the texts. The text is written in using the markdown language. Basically you can just write the text you want. Other caveats about headings, tables, colors, etc. can be found here.

Maths can be typed using LaTeX syntax inside two dollar signs, for example $3^2$ will result in \(3^2\). More information about LaTeX math syntax can be found here.

Finally, you can add R code chunks into the Rmarkdown document by clicking the green +C button under the code file tab and choose R.
Adding a code chunk

Figure B.1: Adding a code chunk

An R code chunk starts with ```{r} and end with three back ticks. You can set some options to the code chunks. Two of the most important options are

  • eval=TRUE: This means that Rmarkdown will evaluate your code when “knitting” the .Rmarkdown file into PDF (or HTML, or Words). If eval=FALSE, then Rmarkdown will not evaluate your code when knitting. You can use this option by typing ```{r, eval=FALSE}. This is useful when you just want to show the code but don’t want to run it.
  • echo=TRUE: This means that Rmarkdown will display your code when “knitting” the file. echo=FALSE means that Rmarkdown will not display your code when knitting the file.
Components of a code chunk

Figure B.2: Components of a code chunk

Finally, you can “knit” your Rmarkdown file into a PDF file by hitting the Knit button on the top bar under the file tabs.

Knitting an Rmarkdown file

Figure B.3: Knitting an Rmarkdown file

Exercise B.1 Create a PDF file that includes the solution (code and output) to Exercises A.1 and A.2.

Notes: To learn more about Rmarkdown, visit https://bookdown.org/yihui/rmarkdown/.