R-

R Defaults

Core R coding conventions covering naming, pipe usage, and idiomatic patterns

Details

Language / Topic
rR
Category
Style Guide

Rules

balanced
- Use `snake_case` for function and variable names — avoid dots in names (e.g., prefer `my_function` over `my.function`).
- Use the native pipe `|>` (R 4.1+) or `%>%` from `magrittr` for chaining data transformations — one transformation per line.
- Use `<-` for assignment instead of `=` — reserve `=` for function argument values.
- Keep functions short and focused on a single transformation — functions that exceed 30 lines should be decomposed.
- Use `TRUE` and `FALSE` instead of `T` and `F` — `T` and `F` can be overwritten as variable names and silently break code.
- Use `seq_along(x)` and `seq_len(n)` instead of `1:length(x)` or `1:n` to avoid off-by-one errors when vectors are empty.
- Wrap script logic in a `main()` function and call it with `if (interactive()) main()` to prevent side effects on `source()`.