Covers: Chapter 16 - R code
R Code Style and Best Practices
Formatting (hard rules)
- Use
<-for assignment;=only for named function arguments. - Indent with 4 spaces. No tabs.
- No lines longer than 80 characters.
- Space around binary operators:
a == b. No space around=in named arguments:somefunc(a=1, b=2). - Full-line comments start with
##, indented to surrounding code.
Naming
- Functions and variables: camelCase starting lowercase (
myFunction). - Classes: CamelCase starting uppercase (
MyClass). - Do NOT put
.in function names (avoids S3 dispatch collisions). - Prefix non-exported/internal functions with a dot:
.internalFunc.
Vectorization and iteration
- Write
seq_len(n)orseq_along(x), NOT1:nor1:length(x)(the latter break when length is 0). - Prefer vectorized code over explicit
forloops. - Use
vapply()instead ofsapply()(type-safe). - Pre-allocate and fill (via
lapply()/vapply()); never copy-and-append in a loop (that is O(n^2)).
Booleans
- Use
TRUE/FALSE, neverT/F.
Functions
- Write small functions; avoid functions longer than one screen.
- Give arguments defaults where sensible; validate with
stopifnot()or checks.
Forbidden patterns
set.seed()inside package/internal code.browser()left in code.- Direct slot access with
@orslot()- use accessor methods instead. <<-(superassignment).system()without justification - usesystem2().- Nested function definitions.
- Commented-out code blocks and TODO comments in published packages.
Messaging
message()for diagnostic messages.warning()for unusual-but-handled situations.stop()for errors.cat()/print()only insideshow()methods, not for general messaging.
Classes and methods
- Prefer S4 over S3. Provide constructor functions and
show()methods. - Use accessors, not direct slot access.
- Only define methods for classes exported by your own package.
- Reuse existing Bioconductor core classes rather than inventing new ones.
Web access, caching, parallelism
- Never write to the user home, working, or installed-package directory.
- Cache downloads via BiocFileCache or
tools::R_user_dir(package, which="cache"); usetempfile()for scratch. - Parallel operations should default to 1 or 2 cores.
Source: R code Fetched 2026-08-14 from contributions.bioconductor.org (Bioconductor devel guide).