Chapter 3 Introduction to RMarkdown

Welcome to the second lab of dapR 1.

This week, we will introduce R Markdown (Rmd), an extremely versatile and powerful tool for writing reproducible documents of all sorts and formats. We promise you that once you get the hang of it, you will never again want to use your ordinary word processor/text editor for writing esssays, coursework, papers, or even presentations or your CV. In fact, all the teaching materials in this course, from the lecture slides to these lab sheets, have been written in Rmd.

By the end of this lab you will:

  • find out about markup languages
  • learn basic text formatting using R Markdown
  • discover the powerful integration of R and text editing that R Markdown offers
  • learn how to write HTML, Word, and OpenOffice files purely in R Studio

 

3.1 What is R Markdown?

Well, it is a language - or a system - for telling computers how to process and format text. Unlike the more familiar WYSIWYG word processors/text editors, R Markdown (as well as other markup languages) uses plain text symbols for all formatting. In other words, there is no higlighting text and then clicking on a B icon to make it bold. What you do instead, that is the topic of today’s lab. At this point you might be thinking “why on Earth should I be learning this when I can just use the text editor on my computer?!”. That is a good question and the answer to it is that, when it comes to Rmd, its intergration with R Studio makes it an incredibly useful tool for wiriting documents that include the results of a statistical analysis or data visualisations. Just how useful Rmd is should become apparent by the end of this lab.

Hold your hats…

 

3.2 Getting things ready

Before you do anything else, open R Studio and install the rmarkdown package if you have not yet done so. This package will enable you to convert files written in Rmd to output of your choice.

Task 1: Type exactly the following command into the console and press ↵ Enter:

install.packages("rmarkdown")

 

 

The best way to learn how R markdown (and programming languages really) works is to compare the source and the output and notice the relationships between the two. So let’s do that and take a look at what the Rmd version of this lab sheet looks like.

Task 2: Click on the Code ▾ button in the top-right corner of this document and select “Download Rmd”. Save the file in your “Week_02”2 folder.

R markdown files have the .Rmd extension after their names though it is possible that your computer is set up to hide file extensions (a setting we would encourage you to change). This file type should be automatically associated with R Studio on your computer. If your computer offers you a selection of programs to open the file in, just choose R Studio and tick the “always use this app/program to open files of this type” box, if there is one.

 

Now, unless you have a two-screen setting where you can put windows side-by-side, you will have to frequently switch between your web browser and R Studio. To save time, here is a handy shortcut: Press Alt + ↹ Tab (Windows) or ⌘ Command + ⇥ Tab (Mac OS)3 to switch between the two most recently viewed windows. If you hold down the first of the two keys, you can toggle all currently open windows by pressing Tab multiple times.

Task 3: Open the Week02_Rmd_into.Rmd file (in R Studio) and try out switching between your browser and R Studio using the key shortcut.

 

3.3 R Markdown basics

Before we delve into the nitty-gritty of Rmd, let’s have a look at how to do some basic formatting you know from your text editor by comparing this section of the the lab sheet and its corresponding source file:


  1. If you have not created a neat folder structure for this course (and all others too!) yet, now is the time to do it. We suggest you create a “Uni” folder whenever you find convenient (e.g., in Documents but please not on your desktop). This folder will store all your files related to your degree. Within it, create a “Year_1” folder, inside of it a “Sem_1” folder, then “dapR_1”, and inside that “Week_01” and “Week_02” folders.

  2. As for Linux, the shortcut depends on your system configuration. Though, if you are using Linux, you probably know how it works.