Explore more RMarkdown functionality and practice implementing it.
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.
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 thst 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:
Compare the output with different numbers of
# in headers
Add empty lines
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.
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:
The markdown syntax to insert an image is:
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
fig.align and add a caption with fig.cap. More details here
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.
Remind yourself about the key code chunk options see overview here
Add a few new code chunks to your document. Name them, add some simple code. Knit and see how the document renders.
Now try changing the code chunk options. Explore the settings to show code, show output, execute output vs not etc.
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?
Add toc details to your YAML header
--- title: "Lab1_doc" author: "Nina" date: "2/12/2021" output: html_document: toc: true ---
More details about styling your table of contents here
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
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
We can also make slides! There are several slide output formats. Try
More details about styling beamer slides here
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:
RMarkdown can be used for much more than making simple documents. In breakout rooms, 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