In this lab, you will be introduced to RMarkdown using RStudio. A great feature of RStudio is that you can create so-called data-driven documents that combine text and analysis into a “reproducible” document. In other words, it allows you to write all of the text, syntax for the analysis, etc. and keep it in a document. This way you are not bouncing between programs and everything is reproducible. This is ideal if you are using a publicly available data set or for making your workflow reproducible.
Why are you learning this? We will be using RMarkdown templates for your homework assignments in this course. When you get to the homework assignments with templates, you will be working with RMarkdown. So, we want to start learning it early!
Also, this is a skill you should develop as you expand your data analysis tool kit.
Data-driven documents are formats that combine text and analysis (i.e. data and code). By combining everything in a single file, we promote transparency and reproducibility. For any given table, figure, or model in the document, you should be able to easily discern how it was created, from what data, and what analysis was used.
We will use the R Markdown format.
All of the document formats build from a simple text formatting convention called markdown.
To create an R Markdown document, you need three things:
Let’s see how it works!
First, you need to install the rmarkdown
and
knitr
packages using:
Now, let’s take a look at how it works. Open a new RMarkdown file using the drop-down menu as shown below:
Then, use the options to pick the type of file you want to create.
Afterwards, click on the knit function in the .Rmd pane:
You did it!
You can select from many different document types, including HTML pages, Microsoft word, presentation formats, or dashboards.
Check out these examples:
R Markdown
Formats
R Markdown
Gallery