RMarkdown,
styler, and lintrYou need to submit your assessments as compiled
RMarkdown .pdfs and adhere to the
tidyverse styleguide. Here’s how!
RMarkdownResearch
Plumbing I includes a brief tutorial on RMarkdown.
RMarkdownSo 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!
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.
Create code cells for all the code blocks.
Double-check that your code still runs and produces the desired output.
Add headings for the exercises to make your document easier to navigate.
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.
To practice typesetting, type-set quarter-over-quarter (QoQ)
growth and annualised QoQ growth using $$.
Knit to .html.
R code: styler and
lintrstyler 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.
stylerInstall the styler package.
Create a .txt file in your project root,
i.e. the file that your R project sits in.
Name it .styler. This file is a very small
customised R script.
In it, write: styler::tidyverse_style(). This sets
it to the tidyverse styleguide. Save and close
.styler.
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”.)
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.
lintrInstall the lintr package.
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).
Fix them one by one, until lintr shows you no more
issues.
Congratulations! You’ve automated adhering to some of the Core ERM marking criteria.