knitr::opts_chunk$set(echo = TRUE)

Coding Guidelines and Best Practices

Except for binary machine code, all computer code is intended to be read by humans.

Summary

Within a CIDA project, code should follow CIDA’s standard organizational structure and be consistently tracked in a Git repository on CIDA’s GitHub server. Strive to use best practices for coding conventions.

CIDA Standards for Organization

File Organization

Files should be organized to fit within the general CIDA file structure. Within the root location of a project should be found the folders:

Within Code/* any code files should be labelled in a usable way and ordered, for example

At the conclusion of a project any files labelled as such should be the last version used; and when used together or in sequence result in the final project results.

Experimental code

Suggestions for easier comprehension

Naming Conventions

Names should follow one of the following naming conventions consistently within a script (with the exception of pre-existing column titles from other databases), and be concise and meaningful. Avoid naming objects the same or similar names to standard functions.

Functions

Commenting

Best practices

This section describes best practices for R, R Markdown, and general coding. They are not meant to be “enforced”, but if followed, they will make your (and your CIDA collaborators’) lives easier later on.

R Scripts (.R files)

R Markdown (.Rmd files)

General best practices for programming

Workflow best practices

Quote from R for Data Science:

One day you will need to quit R, go do something else and return to your analysis the next day. One day you will be working on multiple analyses simultaneously that all use R and you want to keep them separate. One day you will need to bring data from the outside world into R and send numerical results and figures from R back out into the world. To handle these real life situations, you need to make two decisions: 1) What about your analysis is “real”, i.e. what will you save as your lasting record of what happened? 2) Where does your analysis “live”?

Initially, you may consider your analysis to live in your R environment (e.g. the objects listed in the environment pane). However, it’s much easier to recreate this environment from an R script than it is to recreate an R script from the environment! Your analysis therefore lives in your code. So, if you haven’t already, you should instruct RStudio to never preserve your workspace between sessions to foster this attitude and to make your life easier in the long-term.

knitr::include_graphics("figures/coding_guidelines/coding_1.png")

After changing this, you will notice when you restart RStudio that it will not remember the results of the code you ran last time, because remember – your analysis lives in your code.

If you are working within an Rstudio Project, you will not usually have to worry about absolute vs relative file paths (as files will automatically be saved/loaded from the location of the main project (or its subdirectories). However, if you are knitting a R Markdown file in the Reports/* subdirectory and/or loading files elsewhere, you may consider wrapping file paths inside the here() function (from the here package) to ensure all file paths work as anticipated. If your data live on an external server, you may need to point to that server when reading in data (this would be an absolute file path) rather than saving in DataRaw/*. If this is the case, please indicate in the project readme file (or that in DataRaw/* the information on where the data exists (and a contact email address for the owners/maintainers of this server).

Useful reporting functions: The CIDAtools R Package

Where to store your code

Tracking code/files in Git and GitHub

Useful Links

Sample .gitignore file, stored at the project-level directory (the top level of your git repository):

knitr::include_graphics("figures/coding_guidelines/coding_2.png")


CIDA-CSPH/CIDAtools documentation built on July 5, 2025, 1:36 a.m.