Preliminaries

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.).

Exercises

Exercise 1

Create a new 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
The easiest way to create a new R Markdown document is through the RStudio GUI.

Exercise 2

Create/edit the YAML header of the document, so that it includes the following: a meaningful title, an author name, the current date.

We also want a table of contents with two levels and numbered headings.
You can already specify some parts of the 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 — .

Exercise 3

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:

  • set the “penalty to be applied when deciding to print numeric values in fixed or exponential notation” to 10
  • set the number of significant digits to print to 2
  • load the packages 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.

Of course, if you want to use additional/other packages, you can also add those to the setup chunk.

Exercise 4

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.

Please note that in the exemplary 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.

Exercise 5

To further increase reproducibility, let’s add some information about your 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.
You can use a 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.

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.