Skip to Main Content

Datalab: R Markdown

Why use R Markdown?

Markdown is a lightweight markup language which is mostly used to write formatted pages across several platforms like StackOverflow, Github and many others.

Markdown is not only made for programmers but it makes it easy to combine text, references and code pieces and easily convert the documents to formats like PDF, HTML and docs. Another major advantage is the reproducibility and shareability of R Markdown. Combining text and code in one document with introduction, hypotheses, running code and the results of the code makes it easy for others to rerun your work and get the same results as yourself. R Markdown is plain text and works well with version control systems.

 

Getting started with R Markdown

You can use several applications for wiriting Markdown but we will focus on using Markdown in R Studio. 

First you will need to install the package "rmarkdown". This can be done using the command
install.packages("rmarkdown")

When the packages installation is complete you can create a R Markdown document in R Studio the following way:

Go to File -> New File -> R Markdown.
Choose your default output format (this can be easily changed later on)

 

R Markdown - Example

The R Markdown can consist of several types of input and here's an example.

 

Output of markdown

This is an example of an output of the markdown shown in the example. The output is produced in HTML and can be easily published on the web.

R Markdown - Explained

Header

The header is also known as YAML or metadata of the markdown. The header is between the three dashes and this this is where you can specify the title, name, date and output document type. You may as well style your document in many ways and this is also done in the header. Please read more about YAML markdown.

Text 

Text sections can be formatted the way you want by using the markdown syntax. The formatting will be visible in the output of the document. Take a look at the Markdown Cheat Sheet to learn more.

Code chunks

You can use pieces of R code within your document. The code chunks are bounded by triple backtricks and the code pieces can be run directly from the document. The output will be included in the file you create when knitting the document. You can see the sections when you produce your document. The markdown styling and the code will show in the output. Plots, figures and tables will also be produced.

 

Knitting the document

When you are finished with your document you can produce the output by knitting the document. Click the Knit button and save your document as an RMD file. The document will be produced according the output you have set in the header, typically PDF, HTML or doc.

 

 

Course recording - R Markdown

Our R Intermediary course covers R Markdown

 

- The entire course uses R Markdown 

- It shows how to work in the source mode and visual mode

- The course goes through how to write plain text, embed code chuncks and add plots in R markdown, as well as how to knit the document.

- This course also goes through: 

  • R Operators and Functions
  • Correlations
  • Regression Models
  • Statistical Tests
  • Visualizations with ggplot

- Follow this link to download the recording.