R Best Practices

Author

Nicholas Tierney

Published

February 17, 2026

1 Welcome

Welcome to R Best Practices! This course covers essential best practices for writing clear, maintainable, and reproducible R code.

2 Course Materials

Materials will be added here as we progress through the course.

https://r-best-practices.njtierney.com

Prerequisites

  • Basic R programming experience
  • Familiarity with writing R scripts
  • Experience working on data analysis projects

Learning outcomes

  • How to name things effectively
  • Using a style guide
  • How to refactor your code
  • How to review your code and others’
  • How to lay out a project so others know how to run your code
  • How to make a reproducible example (reprex)

3 Schedule

3.1 Project Organisation

  • Common project structures
  • Pitfalls of organisation
  • Understanding file paths
  • RStudio and positron set up
  • “Good Enough”/Common Sense principles of project organisation
  • Always have a README

3.2 Code Style and Readability

  • Style is like grammar
  • why we care about consistent names, spaces, indentation
  • Using the {air} formatter
  • Using linters

3.3 Writing readable code

  • Good vs bad variable names
  • De-chunking code
  • How to review code
  • Review your own code

3.4 Writing Functions

  • The problem functions solve
  • Anatomy of a function
  • Good (simple) function design
  • Using {fnmate} to speed up creating functions
  • How to use a debugger

3.5 Writing a reprex / getting unstuck

  • How to share problems
  • Using {reprex}
  • Practicing reprex
  • Practical tips on debugging
    • Keep solving vs cleaning up

3.6 Putting It All Together

  • Take an existing project and provide code review
  • Apply code linting, code styling, functions
  • Discussion and Q & A