R Best Practices
Welcome
Welcome to R Best Practices! This course covers essential best practices for writing clear, maintainable, and reproducible R code.
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)
Schedule
Project Organisation
- Understanding file paths
- Pitfalls of organisation
- Common project structures
- Naming files
- “Good Enough”/Common Sense principles of project organisation
- Always have a README
- RStudio and positron set up
Code style and readability
- Style is like grammar
- Layout, naming, and correctness are three different jobs
- Why we care about consistent names, spaces, and indentation
- Using the {air} formatter
- Using linters
- Bonus: typing skills and keyboard shortcuts
Writing readable code
- Where we are up to: what the tools fixed, and what they didn’t
- Good vs bad variable names
- De-chunking code
- What to look for: code smells, inputs and outputs, complexity, refactoring
- A process for reviewing code
- Reviewing someone else’s code
- Clean as you go
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
Writing a reprex / getting unstuck
- How to share problems
- Using {reprex}
- Practicing reprex
- Practical tips on debugging
- Keep solving vs cleaning up
Putting It All Together
- Take an existing project and provide code review
- Apply code linting, code styling, functions
- Discussion and Q & A