Good practice R coding

Day 1 - Introduction to Data Analysis with R

Selina Baldauf

Freie Universität Berlin - Theoretical Ecology

October 17, 2026

Chaotic projects and workflows …

… can make even small changes frustrating and difficult.

A frustrated looking little monster in front of a very disorganized cooking area, with smoke and fire coming from a pot surrounded by a mess of bowls, utensils, and scattered ingredients.

Artwork by Allison Horst, CC BY 4.0

Set up your project properly

Consistent structure and filenames

Have a clear project structure

  • One directory with all files relevant for project
    • Scripts, data, plots, documents, …
  • Choose a meaningful project structure 1
  • Keep your raw data untouched
    • data/raw for the original data
    • data/clean for cleaned data created by your scripts
  • Add a readme file (usually README.md) in which you document the project structure
MyProject/
├── data/
│   ├── raw/
│   └── clean/
├── R/
├── figures/
├── MyProject.Rproj
└── README.md

Example RStudio project structure

Use informative file names

  • Human readable: reveal the content
  • Machine readable: no spaces, no special characters
  • Work with default ordering: left-padded numbers or YYYY-MM-DD dates first
MyProject/
├── data/
│   ├── raw/
│   │   └── 2026-10-12_growth-assay.csv
│   └── clean/
│       └── growth-assay_clean.csv
├── R/
│   ├── 01_clean-data.R
│   ├── 02_fit-models.R
│   └── 03_make-figures.R
└── figures/
    └── linear-model-fit.png

Let’s start coding

Good practice R coding

Write beautiful code

  • Try to write code that others (i.e. future you) can understand
  • Follow standards for readable and maintainable code

Artwork by Allison Horst, CC BY 4.0

Standard code structure

  1. General comment with purpose of the script, author, …
  2. library() calls on top
  3. Set default variables and global options
  4. Write the actual code, starting with loading all data files
# Analysis of tree growth data
# Author: Selina Baldauf
# Date: 2026-03-09

library(tidyverse)

# set defaults
input_file <- "data/tree_growth.csv"

# read input
trees <- read_csv(input_file)

# analyse data
summary(trees)

Mark sections

  • Use comments to break up your file into sections
# Load data ---------------------------------------------------------------

trees <- read_csv("data/tree_growth.csv")

# Explore data -------------------------------------------------------------

summary(trees)
  • Insert a section label with Ctrl/Cmd + Shift + R
  • Navigate sections in the file outline

Coding style - Object names

  • Variables should only have lowercase letters, numbers, and *_*
  • Use snake_case for longer variable names
  • Try to use concise but meaningful names
# Good
day_one
day_1

# Bad
DayOne
dayone
first_day_of_the_month
dm1

Coding style - Spacing

Use spaces to make your code more readable:

  • Always put spaces after a comma
  • Spaces around most operators (<-, +, etc.)
  • No spaces around parentheses for normal function calls
# Good
x[, 1]
height <- (feet * 12) + inches
mean(x, na.rm = TRUE)

# Bad
x[,1]
height<-feet*12+inches
mean(x,na.rm=TRUE)

Coding style - Line width

Try to limit your line width to 80 characters.

  • You don’t want to scroll to the right to read all code
  • 80 characters can be displayed on most displays and programs
  • Split your code into multiple lines if it is too long
    • See this grey vertical line in R Studio?

Coding style

Do I really have to remember all of this?

Luckily, no! R and R Studio provide some nice helpers

Coding style helpers - RStudio

RStudio has style diagnostics that tell you where something is wrong

Tools -> Global Options -> Code -> Diagnostics

Coding style helpers - Auto-formatting

RStudio can automatically format your code!

Try it now: open your script from today and use Code -> Reformat Code to auto-format it.

Clean projects and workflows …

… allow you and others to work productively.

But don’t get overwhelmed by all the advice. Just start with one thing.

An organized kitchen with sections labeled "tools", "report" and "files", while a monster in a chef's hat stirs in a bowl labeled "code."

Artwork by Allison Horst, CC BY 4.0