You can find the solutions for this exercise as well as the following ones in the exercises folder within the workshop materials. You can easily copy code from these solution files by clicking on the small blue clipboard icon in the upper right corner of the solution boxes showing the code.
In this exercise, we will create a first R Markdown document. The example data we use here comes from the gapminder package which is based on data from the non-profit with the same name. However, if you prefer to use other or your own data for this exercise as well as the following ones, feel free to do so.
Please also feel free to change and play around with any of the other parameters in this exercise (e.g., output format, chunk options, document content, etc.).
R Markdown document, give it a meaningful name, and save it somewhere in your project folder where you will find it. The output format that we want to use for this exercise is HTML
R Markdown document is through the RStudio GUI.
R Markdown document in RStudio via File -> New File -> R Markdown in the menu.
Create/edit the YAML header of the document, so that it includes the following: a meaningful title, an author name, the current date.
YAML header through the RStudio GUI when creating a new document. To add the current date, you can use inline R code in the respective field within the YAML header. Remember that you need to enclose the YAML header in — .
r Sys.Date()” output: html_document: toc: true toc_depth: 2 number_sections: true
Include a setup chunk in your document. Use this chunk to set the knitr options so that the code is displayed in the output document but messages are suppressed.
Three other things we want to do in the setup chunk:
gapminder and tidyverse
For the general options (i.e., the one not related to knitr), you can check ?options.
You probably do not want to include the chunk options as code output in the resulting HTML document. You can achieve this by setting the chunk option include to FALSE for the setup chunk.
Chunk options: {r setup, include=FALSE} (for the code to be included in this chunk, see the following solution box)
NB: In yourR Markdown document, a code block needs to start and end with three backticks. Remember that in RStudio you can use the keyboard shortcut Ctrl + Alt + I (Windows & Linux)/Cmd + Option + I (Mac) for inserting code chunks.
knitr::opts_chunk$set(echo = TRUE,
message = FALSE)
options(scipen = 10,
digits = 2)
library(tidyverse)
library(gapminder)
Now you can start to create some content for your document. You can get as creative as you want, but the document should at least include the following:
two different levels of headings
some basic text formatting and links
some inline code
code chunks with different options
at least one table
at least one figure
To check if everything works the way you expect, you should knit the document periodically. We have created an exemplary .Rmd file using the Gapminder data. You can find this in the exercises folder.
R Markdown document we use the dplyr package from the tidyverse for data wrangling and ggplot2 for plotting. If you are not used to that or have other preferences, feel free to use base R or other packages of your choosing instead.
gapminder_example.Rmd and the resulting gapminder_example.html for some examples and inspiration.
R session (OS, R version, loaded packages) to the document. Add a section named Reproducibility information at the end of your document and display this information there.
base R function to access information about your session (function name hint 1) and another one for printing it (function name hint 2). You can exclude information about your locale settings there.
sessionInfo() %>%
print(locale = FALSE)
Once you are satisfied with the result (or when we tell you that the time for this exercise is over), knit the document, add the files to Git, commit your changes (adding a meaningful commit message), and push them to your remote repository.