Associated Material

Module: Module 05 - Communicate

Readings:

What is RMarkdown

There are three main components to an RMarkdown document

  1. The YAML header which is surrounded by ---s and provides information for the compiling process
  2. R code chunks which are surrounded by ```s
  3. Text which can be formatted using the Markdown language.

A reference guide of RMarkdown syntax can be found through Help -> Cheat Sheets -> R Markdown Reference Guide in the RStudio menu.

Example Rmd

Create your own RMarkdown document now from the template. To do this go File -> New File -> R Markdown.

Knitting

“Knit” or compile the document using the knit button or Ctrl + Shift + K.


Markdown syntax

Refer to the RMarkdown Reference Guide


Code Chunks

Code chunks look like:

```{r}
1 + 2
```
  • Code chunks are executed as part of the document “knitting” and the code and output embedded in the document

  • Be aware of the working directory (don’t use setwd()). Ideally use the here package to manage paths.


Images, Figures, and Tables

  • Images can be included though markdown, or by using the include_graphics() from knitr in a code chunk - the latter provides more options for customising for size/position.

  • Figures are created from code chunks and chunk options are used to control size/position

  • Tables can be manually created using the markdown syntax or created automatically from your data using the kable() from knitr.

Further customising of kable tables can be done with the kableExtra package.


Citations

Citations can be inserted into an RMarkdown document. This document from RStudio goes through how to do it

Exercises

  1. Create a new RMarkdown file called with the output being HTML.
    1. fill in your name and the title My Report
  2. Save the file as my_report.Rmd and knit it.
  3. Modify the setup code chunk to load the tidyverse package
  4. Remove the template content starting on the line with ## R Markdown and below.
  5. Create a code chunk to read in the gapminder data used in Module 2 - Visualising
    1. Watch out for your file paths relative to your Rmd file
  6. Choose two countries and filter the data to only include them
  7. Make a plot of life expectancy versus gdp per capita
  8. In the text areas of the RMarkdown note some observations about your plot. Include:
    1. a level 2 heading
    2. something in italics
    3. something in bold
  9. Underneath your plot include a table of the data for your countries
LS0tCnRpdGxlOiAiWm9vbSBOb3RlcyAwNTogQ29tbXVuaWNhdGluZyBEYXRhIgpkYXRlOiAiU2VtZXN0ZXIgMiwgMjAyMyIKb3V0cHV0OgogIGh0bWxfZG9jdW1lbnQ6CiAgICB0b2M6IHRydWUKICAgIHRvY19mbG9hdDogdHJ1ZQogICAgdG9jX2RlcHR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