Best Practise for R programming

r
Author
Published

Friday, November 3, 2023

Modified

Friday, February 28, 2025

Writing clean, efficient, and maintainable code is crucial for any programming language, and R is no exception. In this blog post, we will discuss best practices for R programming. These guidelines will help you write better R code, making it easier to read, debug, and share with others. Whether you are a beginner or an experienced R programmer, following these best practices will improve your coding skills and enhance your productivity.

Naming Conventions

  • Variables: Use descriptive names with underscores to separate words (e.g., my_variable).
  • Functions: Use camelCase for function names (e.g., runThisStuff()).
  • Constants: Use all uppercase letters with underscores to separate words (e.g., CONSTANTS).

Code Formatting

  • Indentation: Use 4 spaces for indentation (do not use tabs).
  • Documentation: Always write documentation above function definitions.
  • Function Length: A function should not be longer than one screen.

Efficient Coding

  • Avoid Loops: Avoid using for loops; instead, learn to use lapply and vectorized operations.
  • No Hard-Coded Variables: Never use hard-coded variables in functions.

Code Organization

  • Block Dividers: Use ### ====== to divide function blocks and ### ------ to divide parts within a function.
  • Consistent Naming and Style: Name and style your code consistently.

Memory Management

  • Clean Up: Use rm(list = ls()) and gc() to tidy up memory.
  • Session History: Do not save session history.

Project Management

  • Session Info: Keep track of sessionInfo() in the project folder.
  • Version Control: Use version control systems like Git to manage your code.