Write reliable functions and packages in R
8 steps · about 5 h 20 min · Intermediate
- Design functions around clear inputs (guide, Advanced R (Hadley Wickham), 50 min)
Arguments, return values and environments are the foundation of reusable R code.
- Create functions that retain configuration (guide, Advanced R (Hadley Wickham), 35 min)
A function factory removes repeated setup while making captured state explicit.
- Signal errors and warnings deliberately (guide, Advanced R (Hadley Wickham), 45 min)
Structured conditions let callers recover without parsing message text.
- Build the smallest working package (guide, R Packages (Wickham and Bryan), 45 min)
A complete first pass gives every later file and command a place in the package lifecycle.
- Document exported functions with roxygen2 (guide, R Packages (Wickham and Bryan), 35 min)
Examples and parameter contracts keep the public interface understandable.
- Write focused tests with testthat (guide, R Packages (Wickham and Bryan), 45 min)
Test observable behaviour and edge cases before reorganising the implementation.
- Declare dependencies precisely (guide, R Packages (Wickham and Bryan), 30 min)
Explicit imports make installations reproducible and prevent accidental reliance on attached packages.
- Run the complete package check (guide, R Packages (Wickham and Bryan), 35 min)
Finish with the standard check that catches documentation, namespace and portability problems together.