Welcome to the "Best Coding Practices in R" book! This book provides a comprehensive guide to best coding practices in R, focusing on essential aspects like script organisation, naming conventions, commenting, and version control. It aims to enhance the readability, maintainability, and collaborative potential of R code by offering practical advice on creating well-structured and clean scripts.
- 
Introduction
- Why Coding Practices Matter
 - What This Book Covers
 - How to Use This Book
 - Who This Book Is For
 - Final Thoughts
 
 - 
Naming Conventions
- Naming Scripts
 - Naming Variables
 - Naming Functions
 - Consistency is Key
 - Summary
 
 - 
Organising Scripts
- Structuring Scripts
 - Organising Files and Directories
 - Documentation
 - Summary
 
 - 
Commenting Code
- The Purpose of Comments
 - Best Practices for Commenting
 - Common Pitfalls to Avoid
 - Advanced Commenting Techniques
 - Summary
 
 - 
Code Syntax and Spacing
- Importance of Consistent Syntax
 - Indentation
 - Spacing
 - Line Breaks
 - Aligning Code
 - Comment Placement and Spacing
 - Avoiding Common Pitfalls
 - Summary
 
 - 
Writing Functions
- The Purpose of Functions
 - Structure of a Function
 - Documentation and Commenting
 - Testing Functions
 - Advanced Function Techniques
 - Avoiding Common Pitfalls in Function Writing
 - Summary
 
 - 
Error Handling
- Introduction to Error Handling
 - Basic Error Handling with 
tryandtryCatch - Custom Error Messages with 
stop,warning, andmessage - Defensive Programming
 - Best Practices for Error Handling
 - Summary
 
 - 
Version Control with Git
- Introduction to Git
 - Setting Up Git
 - Basic Git Workflow
 - Branching and Merging
 - Working with Remote Repositories
 - Advanced Git Features
 - Git in RStudio
 - Best Practices for Using Git
 - Summary
 
 - 
Reproducibility and Documentation
- Introduction to Reproducibility
 - Setting Up a Reproducible Environment
 - Sharing Your Work
 - Documentation for Reproducibility
 - Case Study: Reproducible Analysis in R
 - Summary
 
 - 
Conclusion
 
- Recap of Key Concepts
 - The Importance of Adopting Best Practices
 - Moving Forward
 - Final Thoughts
 
You can access the full book online via the following link: Best Coding Practices in R
To view the book locally, you will need to clone this repository and render the Bookdown project. Follow these steps:
- 
Clone the Repository
git clone [https://github.com/MOPAC-DS/Learning-Resource-Best-Coding-Practices-in-R.git](https://github.com/MOPAC-DS/Learning-Resource-Best-Coding-Practices-in-R)
 - 
Install Required Packages
 
Make sure you have R and the necessary packages installed. You can install them using:
install.packages(c("bookdown", "tidyverse", "ggplot2"))- Render the Book
 
Open R or RStudio and set your working directory to the cloned repository. Then run:
bookdown::render_book("index.Rmd")This will generate the book in various formats (HTML, PDF, etc.).
Contributions are welcome! If you have suggestions for improvements or spot any issues, please open an issue or submit a pull request.
For questions or feedback, please contact Daniel Hammocks via email daniel.hammocks@mopac.london.gov.uk.
This book is licensed under the MIT License.