Preliminaries

You can find the solutions for this exercise as well as the following ones in the exercises folder in the workshop material. You can also navigate the workshop material online (including exercises and solutions).

You can copy code from the exercise and solution files by clicking on the small blue clipboard icon in the upper right corner of the boxes showing the code.

Some of the exercise require you to add text and R code. Feel free to copy from the example paper in the folder exercises/example_manuscript. We do, however, encourage you to use material from a project of your own, if possible. It will be a lot more fun.

Exercises

In this exercise, you will create a first R Markdown document. The example data we use here is taken from the example manuscript provided with the workshop material. However, we encourage you to use your own data for this and the following exercises.

Exercise 1

Create a new R Markdown document with HTML output format and knit it.



YAML front matter

Exercise 2

Customize the YAML front matter of your R Markdown document. Add

  1. a meaningful title
  2. an author name
  3. an abstract field to the YAML front matter
  4. a floating table of contents, and
  5. choose a different theme.
Feel free to customize some more! :)
See the documentation for rmarkdown::html_document().



Markdown

Exercise 3

Add some text to the document. You can copy the example paper introduction or text for a project of your own. Try out some of the text formatting, e.g.,

  • create headings,
  • make some words bold or italic,
  • add an unordered list,
  • add a footnote,
  • add citations, or
  • add an equation.
Try using the visual editor (or citr) for some of your editing.

You can find a quick reference guide to markdown formatting syntax in RStudio. Go to Help > Markdown Quick Reference.

To switch to the visual editor click the button at the top of your Rstudio editor pane.

Don’t forget to add the bibliography file to the YAML front matter for your citations to render properly.



R code

Exercise 4

Add some analyses code to the document. You can copy the analysis code for the example paper or use code for a project of your own.

  • Create separate chunks for meaningful sets of analytic steps.
  • Add descriptive chunk names.
  • Include a hidden code chunk that creates plot.
Make sure that you use relative paths to access any data files. Ideally place your R Markdown document and the data in a common parent diretory.

Insert a new code chunk either by clicking , or with Ctrl + Alt + I.

Be careful not to break code chunks. The enclosing elements ```{r} and ``` have to be on lines of there own and cannot be preceeded by white space.

See knitr documentation for a complete list of chunk options.



Bonus

Bonus 1

Add a setup chunk at the top of your document. In this chunk

  1. load the knitr package and
  2. set the default chunk options to hide all messages and warnings.

You can insert a new code chunk by clicking or with the keyboard shortcut Ctrl + Alt + I.

See the documentation for knitr::opts_chunk.



Bonus 2

To document the versions of all packages that you used for the analysis, let’s document your R session (operating system, R version, R packages, etc.).

Add a section named Computational environment at the end of your document and report your session information.
See the documentation for sessioninfo::session_info().



Bonus 3

Create a brief presentation of your findings. Use R Markdown to either create

There are many great examples which you can adapt online.

xaringan builds on a powerful JavaScript library called remark.js. This library has many powerful advanced features. If this is something you are interested in, I recommend the following resources:

Solutions

After you have tried yourself, feel free to have a look at our solutions.