Goal for today

Explore more RMarkdown functionality and practice implementing it.



Exercise 1: Basic formatting and code chunk options

Let’s start by playing around with the basic RStudio RMarkdown template a little more. Open a fresh copy by clicking File -> New File -> R Markdown… We can enter “Lab exercise” in the Title field and keep HTML as the Default Output Format.


Basic formatting

Let’s play around with the formatting of our document. Remember that you can find RStudio’s built-in RMarkdown reference under Help > Markdown Quick Reference, or check out one of the many great cheatsheets you can find online, e.g. this or RStudio’s RMarkdown cheatsheet you can download from here (scroll down to find it).

As you’re making changes, knit often and see how your edits look in the rendered document.

Some ideas of what you can try:


Add different header levels

Compare the output with different numbers of # in headers


Blank space

Add empty lines <br>


In-line code

Another really cool and useful functionality is writing in-line code. Code results can be inserted directly into the text of a .Rmd file by enclosing the code with r. This way, you’re basically running code inside a sentence. R Markdown will always display the results of in-line code, but not the code, and apply relevant text formatting to the results. As a result, inline output is indistinguishable from the surrounding text, but will show up-to-date results of your code.

We may for example want to summarize the dimensions of our dataset. We can do this by typing

There are `r nrow(cars)` observations in the cars dataset, 
and `r ncol(cars)` variables.

See how that renders.


Add images

In addition to figures generated through R code in our code chunks, it’s easy to also add other images, e.g. jpg or png files that you have stored on your computer or find online.

There are two ways we can do this:

  1. With markdown syntax
  2. With knitr::include_graphics()


The markdown syntax to insert an image is: ![caption]("path/to/image")

The path can either be a path to an image stored on your computer, or it can be a URL to an image stored online. If you want to get the URL to an image you find, right click and select “Copy Image Address”.


Using knitr::include_graphics(): If you want more control over the output, e.g. to center the image or make it smaller, you can use knitr::include_graphics(), and control the figure size using options such as out.width and fig.align and add a caption with fig.cap. More details here


Center and right align text

You cannot align text with the native markdown syntex. However, depending on the output format, you can use other methods. For example, if you are knitting the RMarkdown file into an HTML file, you can use HTML tags such as the following:

<center> Centered text (HTML) </center>
[Aligned right (HTML)]{style="float:right"}

Alternatively, if you are knitting the RMarkdown file into a PDF file, you can use LaTeX commands. E.g.,

\center Centered text (PDF) \center
\hfill Aligned right (PDF)


Equations

Want to include equations in your writing? Easy. rmarkdown supports LaTeX style equation writing. We won’t go through this in class, but you can find an easy introduction in R Markdown for Scientists.


Code-chunk options

Remind yourself about the key code chunk options, see overview here

  1. Add a few new code chunks to your document. Name them, add some simple code. Knit and see how the document renders.

  2. Now try changing the code chunk options. Explore the settings to show code, show output, execute output vs not etc.

  3. Now explore the first code chunk titled setup. What happens if you change it to echo = FALSE? Or if you add eval = FALSE? Why does this happen?



Exercise 2: Document styling and output formats

Add table of contents

Add toc details to your YAML header

---
title: "Lab1_doc"
author: "Nina"
date: "2/12/2021"
output:
  html_document:
    toc: true
    toc_depth: 2
---

More details about styling your table of contents here



Output formats

Up until now, we’ve been rendering our .Rmd file to the default html format. That’s useful for lots of applications. But lots of other output formats are supported.

Check what happens if you change the output: html_document in the YAML header to output: word_document

Now, check what happens if you change it to output: pdf_document. In order to convert the documents to PDF, they use a software called LaTeX (pronounced la-tek or lay-tek).

Installing LaTeX can be a pain, but thankfully there is an easier way to install it - tinytex. tinytex is an R package that installs a sane, lightweight (<200Mb) version of LaTeX.

If you’re having trouble with the generating the pdf, see the note on installing tinytex here.


We can also make slides! There are several slide output formats. Try output: beamer_presentation

More details about styling beamer slides here



Exercise 3: Make a CV

Now, practice what you’ve learned by creating a brief CV. The title should be your name, and you should include headings for (at least) education or employment. Each of the sections should include a bulleted list of jobs/degrees. Highlight the year in bold and add a footnote. You can also add a photo, either of yourself, or something else you have handy or can find online.

Render to html format, and if you’re comfortable, share with the class by posting it to the lecture-chat channel in Slack.

You will have seen how quickly you can create a nicely formatted document. But there are also lots of great CV templates out there that we can use. You could, for example, try one of these:

http://svmiller.com/blog/2016/03/svm-r-markdown-cv/ http://nickstrayer.me/datadrivencv/



Exercise 4: Explore other RMarkdown applications

RMarkdown can be used for much more than making simple documents. In small groups, explore examples of interactive documents, dashboards, books, websites, rticles, and more. You can find examples of all of these in the RStudio Gallery

Discuss in your groups which application you think is the coolest, and report back to the group.



END LAB 1