Write reliable functions and packages in R

8 steps · about 5 h 20 min · Intermediate

  1. Design functions around clear inputs (guide, Advanced R (Hadley Wickham), 50 min)

    Arguments, return values and environments are the foundation of reusable R code.

  2. Create functions that retain configuration (guide, Advanced R (Hadley Wickham), 35 min)

    A function factory removes repeated setup while making captured state explicit.

  3. Signal errors and warnings deliberately (guide, Advanced R (Hadley Wickham), 45 min)

    Structured conditions let callers recover without parsing message text.

  4. 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.

  5. Document exported functions with roxygen2 (guide, R Packages (Wickham and Bryan), 35 min)

    Examples and parameter contracts keep the public interface understandable.

  6. Write focused tests with testthat (guide, R Packages (Wickham and Bryan), 45 min)

    Test observable behaviour and edge cases before reorganising the implementation.

  7. Declare dependencies precisely (guide, R Packages (Wickham and Bryan), 30 min)

    Explicit imports make installations reproducible and prevent accidental reliance on attached packages.

  8. 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.