Style guide ๐ง๐ปโ๐ป
R style guide โ
pipes โ |> ยท ๐ฌ๏ธ
- one line for each step in a pipe
- first line pipe with name of new object and assignment operator (only)
- use Base-R pipe
|>over Tidyverse-R pipe%>%for R >= 4.2- placeholder
_(|>) instead of.(%>%) - check Use native pipe operator in RStudio
- placeholder
data ยท ๐ข
- use coherent prefixes for groups of data //
dt dt_ctry dt_ctz - prefix
raw_for raw data read into object with
plots ยท ๐
- prefix
plfor plot objects //pl pl1 pl2 - data for plot only into
pl_dtobject
models ยท ๐ฌ
- prefix
mofor model objects //mo mo_lm mo1
workflow ยท โ๏ธ
- .gitignore sources with
source__prefix- [ single option to ignore copyright protected, personal or sensible sources ]
- final checks
- restart R session and run all
- check all object names
- check console messages and warnings
- reproducible environments with Rocker
- see
Dockerfileanddocker-compose.ymlexamples
- see
packages ยท ๐ฆ // see snippet below
- use conflicted to avoid function name conflicts
- load first with
library(conflicted) - specify preferred functions (e.g.
conflicts_prefer(dplyr::filter()))
- load first with
- load
library(tidyverse)afterconflicted - add brief description why package is loaded
library(broom) # models // tidy results
- order packages loaded by thematic groups
- plot โ patchwork ggrepel
- models โ broom ยท ggeffects
- specify single usage of package with
::- comment below loading packages block //
# janitor:: - no comment for usage tidyverse of packages that are not loaded with library(tidyverse) (e.g. glue readxl)
- [ reduce potential name conflicts and code completion options ]
- comment below loading packages block //
Snippets ยท โ๏ธ
Loading packages
library(conflicted) # create errors for function name conflicts
conflicts_prefer(dplyr::filter, .quiet = TRUE)
library(tidyverse)
# order alphabetically in section and provide category with brief description
library(broom) # models // tidy results
library(ggeffects) # models // visualize model effects
library(patchwork) # plots // arrange plots
library(reactable) # layout // interactive data tables
library(sf) # maps // spatial data tools
# DT:: skimr:: viridis::Note โ These are my personal best practices for code formatting in Tidyverse R. They have evolved over time and not all my previous code may follow these guidelines.