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.
Me when I revisit my code from a few months ago:
— Emine Fidan (@Em_The_Engineer) March 1, 2019
I’m getting to be more thorough with my comments, I promise! 😫 #DataScience #rstats #BiosystemsAnalyticsLab pic.twitter.com/NiXRTorbhd
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:
- Insert chunks to load the tidyverse package and read in the sydneybeaches data
- Insert chunks to recreate your favourite plot from VizW(h)iz
- 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!