You need to submit your assessments as compiled RMarkdown .pdfs and adhere to the tidyverse styleguide. Here’s how!


RMarkdown

Research Plumbing I includes a brief tutorial on RMarkdown.

Exercise 1: Getting comfortable with RMarkdown

So far, you’ve saved your solutions for the Fredr and Lakisha problem sets in .R files. Now it’s time to turn them into .Rmd files!

  1. Create new .Rmd files for both and save them in your Core ERM folder. Select .html as the default output for now, and enter your candidate number under “Author” to guarantee anonymity for marking. Then copy-paste your solutions.

  2. Create code cells for all the code blocks.

  3. Double-check that your code still runs and produces the desired output.

  4. Add headings for the exercises to make your document easier to navigate.

  5. Set some text in italics and bold some other text for emphasis. When you refer to any R syntax in your text, use `` around it.

  6. To practice typesetting, type-set quarter-over-quarter (QoQ) growth and annualised QoQ growth using $$.

  7. Knit to .html.


Writing clean R code: styler and lintr

styler and lintr are two complementary packages that help you write clean and correct R code — explicit marking criteria for this course!

  • styler is an auto-formatter: it rewrites your code to fix spacing, indentation, quote style, line breaks, and operator alignment. It changes your file for you. Think of it as a robot proofreader that handles all the cosmetic issues automatically.

  • lintr is a static analyser: it reads your code and flags problems that styler cannot fix automatically — things like overly long lines, non-snake_case variable names, or use of = instead of <- for assignment. It reports issues but leaves the fixing to you.

The typical workflow is: run styler first to auto-correct formatting, then run lintr to catch whatever is left.

Exercise 2: styler

  1. Install the styler package.

  2. Create a .txt file in your project root, i.e. the file that your R project sits in.

  3. Name it .styler. This file is a very small customised R script.

  4. In it, write: styler::tidyverse_style(). This sets it to the tidyverse styleguide. Save and close .styler.

  5. In R, run “Style active file” under “Add-ins”. (You might have to start a new R session for styler to appear under “Add-ins”.)

  6. Done! You are now adhering to most of the tidyverse styleguide automatically: “Style active file” automatically corrects your spacing, indentation, line breaks, placement of wrappings, consistent quotes, etc. Note, however, that “Style active file” does not fix bad variable names, inefficient code, logical errors, or bugs.

Exercise 3: lintr

  1. Install the lintr package.

  2. Run “Lint current file” under “Add-ins”. You will see all the remaining problems with your code in the Markers pane (the tab next to Console at the bottom of RStudio).

  3. Fix them one by one, until lintr shows you no more issues.

  4. Congratulations! You’ve automated adhering to some of the Core ERM marking criteria.