MarkyMark 2

Code Chunks in R Markdown

The real power of R Markdown comes when you start combining text and code. There are MANY advantages of putting notes alongside the code you are running. Even if your future self is the only person who benefits from those notes, they are invaluable. By using code chunks, R Markdown keeps the code with the output too, so someone else looking at your analysis can see what code you ran and what output you got.

In this lesson, we are going to walk through how to use code chunks in R Markdown to create a reproducible report.

Lesson Outcomes

By the end of the lesson, you should be able to:

  • 2.1 Insert chunks of code in your R Markdown document
  • 2.2 Use the chunk settings to control what appears in your report

How to insert code chunks

In this screencast, we’ll cover:

  • How to insert code chunks
  • How to control what appears in your document when you knit

Watch the video and then carry out the following steps:

  1. Insert chunks to load the tidyverse package and read in the sydneybeaches data
  2. Insert chunks to recreate your favourite plot from VizW(h)iz
  3. Knit your document, changing the chunk settings so that only the code and output appear in your document.

Sydney-based R-Ladies, head on over to the #ryouwithme_4_markymark channel in Slack to share your code chunk successes (or woes!).

Next up, Lesson 3 tackles export formats!