From 89e7899b6d7a41d33248dc1d3777b09d02585015 Mon Sep 17 00:00:00 2001 From: lukabor Date: Mon, 8 Jun 2026 22:58:21 +0200 Subject: [PATCH] r-style/tidyverse: add native pipe, \(x) shorthand, snake_case, roxygen2 --- skills/engineering/r-style/SKILL.md | 6 ++ skills/engineering/r-style/tidyverse.md | 88 +++++++++++++++++++++++++ 2 files changed, 94 insertions(+) diff --git a/skills/engineering/r-style/SKILL.md b/skills/engineering/r-style/SKILL.md index 7d66c12..2114cde 100644 --- a/skills/engineering/r-style/SKILL.md +++ b/skills/engineering/r-style/SKILL.md @@ -26,6 +26,12 @@ description: Project R coding standards — Tidyverse style, `box::use` for impo `data.table`-class objects above ~1 M rows, wrap with `data.table::lazy_dt(df)` and exit with `as_tibble()`. See [dtplyr.md](dtplyr.md). +5. **Native pipe `|>` only.** No `%>%`. Anonymous functions: `\(x)`, + not `function(x)`. See [tidyverse.md §5–6](tidyverse.md). +6. **`snake_case` for names, `SCREAMING_SNAKE_CASE` for constants.** + No `camelCase`, no `dot.separated`. See [tidyverse.md §7](tidyverse.md). +7. **Roxygen2 on every exported function** in a package + (`DESCRIPTION` present). See [tidyverse.md §8](tidyverse.md). ## Tooling, not enforcement The skill states the rules; `styler::style_pkg()` and `lintr` enforce them. diff --git a/skills/engineering/r-style/tidyverse.md b/skills/engineering/r-style/tidyverse.md index 0747d9d..092d7a1 100644 --- a/skills/engineering/r-style/tidyverse.md +++ b/skills/engineering/r-style/tidyverse.md @@ -97,6 +97,94 @@ load_hippie() See [box.md](box.md). `source` is invisible imports + arbitrary side effects; `box::use` is named imports + tested boundaries. +## 5. Native pipe `|>` only — no `%>%` + +Use the base R native pipe. `magrittr`'s `%>%` is an extra dependency +with subtly different semantics (`.` placeholder, lazy evaluation, S4 +method dispatch quirks). + +### Bad +```r +df %>% filter(x > 0) %>% select(a, b) +``` + +### Good +```r +df |> filter(x > 0) |> select(a, b) +``` + +When you need a placeholder, name the argument: + +```r +df |> filter(x > 0) |> with(cor(a, b)) # WRONG — looks like magrittr `.` +df |> filter(x > 0) |> (\(d) cor(d$a, d$b))() # RIGHT — explicit lambda +``` + +## 6. Anonymous functions — `\(x)` over `function(x)` + +The native shorthand is shorter, scans faster inside a pipe, and is +unambiguous in argument position. + +### Bad +```r +list |> map(function(x) x^2) +``` + +### Good +```r +list |> map(\(x) x^2) +``` + +`function(x)` is fine for top-level definitions (`flatten_one <- +function(v) ...`); the shorthand is the default *inline*. + +## 7. Naming — `snake_case` for everything except constants + +```r +# Functions, variables +calculate_mean <- function(x) mean(x) +dataset_ids <- c("DS001", "DS002") + +# Constants +MAX_ITERATIONS <- 1000 +DEFAULT_TIMEOUT <- 30L +``` + +Anti-patterns: `camelCase`, `dot.separated` (legacy base R idiom), and +`PascalCase` for anything that isn't an S4 class. + +```r +# WRONG +calculateMean <- function(x) mean(x) +my.helper <- function(x) x +maxIterations <- 1000 +``` + +## 8. Roxygen2 docs on every exported function (packages only) + +In an R package (`DESCRIPTION` present), every `@export`ed function +needs a roxygen2 block. Markdown enabled. + +```r +#' Calculate the mean of a numeric vector +#' +#' @param x A numeric vector. +#' @param na_rm Logical. Remove NAs before computing? Default `TRUE`. +#' @returns A scalar numeric. +#' @export +calculate_mean <- function(x, na_rm = TRUE) { + mean(x, na.rm = na_rm) +} +``` + +Minimum required tags: title (the first line), `@param` for every +argument, `@returns`, `@export`. `@examples` when the function is +non-trivial. No docs is a `devtools::check()` warning. + +In `box::use`-based projects without a `DESCRIPTION`, roxygen2 is +optional — but a one-line `#'` comment over every exported function is +still strongly recommended. + ## Tooling - `styler::style_pkg()` — auto-format to Tidyverse rules.