Style Conventions
in R Programming
Learn to write clean, readable, and consistent R code by following the tidyverse style guide: naming, spacing, indentation, and more.
Why Does Code Style Matter?
Code is read far more often than it is written. Consistent style conventions make your R programs easier to understand, debug, and share with collaborators. Style is not about personal preference: it is about communication.
The Tidyverse Style Guide
The tidyverse style guide is the most widely adopted set of conventions for writing R code. Created by Hadley Wickham, it covers everything from how you name variables to how you indent your pipes.
Anatomy of Well-Styled Code
Core Rule: snake_case Naming
The tidyverse style guide mandates snake_case for all object names: variables, functions, and arguments. Words are separated by underscores and everything is lowercase.
student_count
calc_mean_score
is_valid
dayone (no separator)
day.one (dots)
day1 (ambiguous)
Core Rule: Spacing Around Operators
Always place spaces around operators like <-, =, +, -, ==, and after every comma. This makes expressions easier to scan visually.
total <- a + b
mean(x, na.rm = TRUE)
total<-a+b
mean(x,na.rm=TRUE)
<- vs =: Assignment Style
Both <- and = can assign values in R, but they have different conventions and subtle differences in behavior.
| <- (arrow) | = (equals) | |
|---|---|---|
| Convention | Tidyverse standard | Less common for assignment |
| Global Assignment | ✓ Works everywhere | ✓ Works everywhere |
| Inside function() | Assigns variable in current scope | Sets argument only |
| Readability | Clear intent | Can confuse with == |
| Keyboard shortcut | Alt + - (RStudio) | n/a |
Indentation & Line Length
result <- data |>
filter(age > 18) |>
select(name, score) |>
arrange(desc(score))
How to Apply Style Conventions
Example 1: Fixing Names
A student wrote the following code. Let's restyle it following the tidyverse guide.
Total.Count <- nrow(myData)
avgScore <- mean(myData$Score)
total_count <- nrow(my_data)
avg_score <- mean(my_data$score)
Example 2: Spacing & Pipes
This code runs correctly but violates several spacing and structure rules.
filter(age > 18) |>
select(name, score) |>
arrange(desc(score))
Example 3: Function Definitions
Functions deserve special care. Name them as verbs, document their purpose, and format the body consistently.
x+y/2}
calc_midpoint <- function(x, y) {
(x + y) / 2
}
Writing Good Comments
library(tidyverse)
# Import data ----
# Using read_csv for automatic type detection
raw_data <- read_csv("survey_results.csv")
File Naming & Organization
File names follow the same snake_case rules as variable names, with a few extra conventions for projects.
02_clean_data.R
03_analysis.R
utils_plotting.R
stuff.R
my script (1).R
Code.R
Common Mistakes
Style Cheat Sheet
| Convention | Do | Don't |
|---|---|---|
| Variable names | snake_case | camelCase, dot.case |
| Assignment | <- | = |
| Spacing | x <- y + z | x<-y+z |
| Indentation | 2 spaces | tabs or 4 spaces |
| Line length | < 80 characters | unlimited |
| Pipe placement | end of line | start of line |
| Comments | # space then text | #no space |
| File names | 01_clean_data.R | Clean Data.R |
Question 1 of 5
Which of the following variable names follows the tidyverse style guide?
Question 2 of 5
What is the recommended assignment operator in the tidyverse style guide?
Question 3 of 5
A student writes the following R code:
result<-data%>%filter(score>=90)%>%select(name,grade)
How many style violations does this single line contain?
Question 4 of 5
What is the recommended indentation size in the tidyverse style guide?
Question 5 of 5
Which comment style follows tidyverse conventions?