1 Purpose and Goals

This tutorial is intended for students in ECS 132: Probability and Statistical Modeling for CS, offered by the department of Computer Science at UC Davis. After this tutorial, you will learn:

  1. What RMarkdown is and why do we care?
  2. Learn how to write your homework in RMarkdown
  3. Learn to Embed LaTeX code within your R Markdown file
  4. How to submit your homework assignments

2 What is R Markdown?

Simply put, R Markdown is a file format to help you create dynamic documents, but with R. It uses simple Markdown and embeddable code chunks written in R. It also allows you to use LaTeX to typeset mathematical equations. Overall, R Markdown produces beautiful documents and academic reports.

We will be using R Markdown to write up homework solutions. Since R Markdown requires some prior knowledge or familiarity with Markdown and LaTeX, please make sure you check out how to write in Markdown syntax, and basic typesetting using LaTeX syntax.

3 Downloading R-Studio and R Markdown

  1. R Markdown is an R Studio plugin/extension. Be sure to download R Studio here

  2. Follow steps to finish setting up your R Studio IDE.

  3. Once you have set up the IDE, type the following on your R Studio console that you should see on your screen (or on the bottom of your IDE if you have a file open).

Console in R Studio

Console in R Studio

  1. Congratulations! You have successfully installed R Markdown.

3.1 TeX Engines

Since students are expected to submit PDF files, R Markdown requires a TeX engine installed. If you are using Windows, please make sure you have MiKTeX installed. Alternatively, if you are using a Mac, make sure to install MacTeX.

4 Creating your first R Markdown (.Rmd) File

  1. To create a new .Rmd file in R Studio, select File >> New File >> R Markdown.

    1. Fill in the Title and Author for your .Rmd file.

    2. Select the file type. Since you’re using R Markdown to submit homework, select PDF

  2. By doing this, a minimal R Markdown file will be created for you. Here is how it should look.

Default RMD File Generated by R Studio

Default RMD File Generated by R Studio

  1. If you want to see the PDF the document yields, click on the Knit button.

  2. Congratulations! You’ve knitted your first .Rmd file to create a minimal PDF file.

4.1 The YAML Header

The first few lines in your .Rmd file that you see encapsulated within the --- lines, is known as the YAML Header. This defines the required headers for your .Rmd file. For our homework assignments, shall use the following header:

Replace the default YAML header with the following:

This should be self explanatory. We just define a title for our homework, the author, the date (whenever you knit), and define a default Table of Contents. There might be some pieces inside the author section that look strange to you. This is because, we can only define new lines and paranthesis using LaTeX syntax.

4.2 .Rmd File for Homework

  1. To make things easier, we have provided you with a template for writing homework. Please follow the template to write your homework. You can find the file in Canvas or here.

  2. Simply replace the existing contents with the above mentioned .Rmd contents from the template file.

  3. Knit the file to a PDF and confirm that everything is fine. You should now have a template homework PDF.

You should see something like this:

5 Typsetting Mathematical Equations using LaTeX

Often times you want to write out mathematical equations or proofs. If you want to add typset mathematical equations inline do so by typing LaTeX code in between two $ symbols

$ <Latex code> $

For example: The following LaTeX code

Let $P$ be the probability that I milk a cow. Then $Q = 1 - P$ is the probability that I don't milk a cow.

yields the following:

Let \(P\) be the probability that I milk a cow. Then \(Q = 1 - P\) is the probability that I don’t milk a cow.

5.1 Writing Multiple Line Equations Using LaTeX

If you want to write mathematical equations that span more than just a single line, you can do so like this:

$$
<Multi Line Latex Code Here>
$$

For Example: The following LaTeX code

Let $P(A) = 0.5$, $P(B) = 0.2$, $P(A \cap B) = 0.3$.

Then
$$
\begin{split}
P(A \cup B) &= P(A) + P(B) - P(A \cap B) \\
&= 0.5 + 0.2 - 0.3 \\
&= 0.4
\end{split}
]
$$

yields the following:

Let \(P(A) = 0.5\), \(P(B) = 0.2\), \(P(A \cap B) = 0.3\).

Then \[ \begin{split} P(A \cup B) &= P(A) + P(B) - P(A \cap B) \\ &= 0.5 + 0.2 - 0.3 \\ &= 0.4 \end{split} \]

Now, try to create your own multi-line LaTeX equation and embed it in your .Rmd file.

6 Using and Embedding R Code Chunks

Often times, you will be asked to write some sort of R code in your homework. You want to embed your R code in your .Rmd file. Any R code that is to be writted and embedded in your .Rmd file, should be enclosed within three grave accents that look like this: ` ` `

Here is an example of a code chunk:

{r someOptionalChunkName, eval=TRUE} n <- rnorm(10)

When you write a code chunk like this, R Markdown, by default, compiles the code, executes it, and writes the output to the file. If you want to suppress the output, then you can write a code chunk like this:

{r someOtherChunkName, resuls='hide'} n <- rnorm(10)

If you do not wish to execute the code and just print out the code chunk, then you can write a code chunk like this:

{r iCantThinkAnymore, eval=FALSE} n <- rnorm(10)

Note: Refer to Homework_Template.Rmd located in Canvas for a better understanding.

7 Submitting Homework

Homework will be turned in on Canvas. Please make sure you turn in BOTH, the .Rmd file and the PDF file it yields.

Please make sure you keep the header # Classmate Collaborators at the end of of your .Rmd file. Indicate the name and the email of the student(s) you collaborated with. You are encouraged to collaborate with your peers but everyone’s work must be individual.

Turning in identical PDF files is considered plagiarism and will result in a violation in academic conduct.

8 Final Words

Hopefully this simple tutorial should have gotten you started with getting your homework template .Rmd file to knit to a PDF. If you did, congrats! You can start working on the homework now :)

If you have any questions, please post your questions to our Piazza forum.