.github/CODING_STYLE.md

Coding Style Guide

General Rules

# GOOD
x <- 1

# BAD
x = 1
1 -> 1
# GOOD
mean(x, na.rm = TRUE)

# BAD
mean(x, na = TRUE)
# GOOD
raise_to_power(x, power = 2.7)

# BAD
raise_to_power(power = 2.7, x)
# GOOD
standardize(..., scale = TRUE, center = TRUE)

# BAD
standardize(scale = TRUE, ..., center = TRUE)
# GOOD
ggplot2::ggplot()
dplyr::select()

# BAD 
ggplot()
select()
# GOOD
x <- 1
x <- x + 1

# BAD 
x <- 1; x <- x + 1

Naming

As a general rule, abbreviations must be avoided when naming.

Naming Files

# GOOD
plot.R

# BAD
plot
# GOOD
plot.R

# BAD
Untitled1.R
# GOOD
read_csv.R
plot-methods.R

# BAD
read csv.R
# GOOD
plot.R

# BAD
Plot.R
données.R
# GOOD
fit_model.R

# BAD
addition.R

Naming Variables

# GOOD
std_dev <- 3

# BAD
std.dev <- 3
StdDev <- 3
# GOOD
std_dev <- 3

# BAD
T <- 1
c <- 2 * 2
mean <- 10

Naming Functions

# GOOD
peak_detect()

# BAD
addition()
readFile()

Naming S4 Classes

Syntax

Line Length

Spacing

# GOOD 
x == y
z <- 2 + 1

# BAD
x==y
z<-2+1
# GOOD 
mean(x = c(1, 2, 3), na.rm = TRUE)

# BAD
mean(x=c(1, 2, NA), na.rm=TRUE)
# GOOD
car$cyl
dplyr::select()
1:10

# BAD
car $cyl
dplyr:: select()
1: 10
# GOOD 
mtcars[1, ]
mean(x = c(1, NA, 2), na.rm = TRUE)

# BAD
mtcars[1 ,]
mean(x = c(1,NA,2),na.rm = TRUE)
# GOOD 
for (element in element_list)
if (total == 5)
sum(1:10)

# BAD
for(element in element_list)
if(total == 5)
sum (1:10)
# GOOD 
if (is_true) message("Hello!")
species["tiger", ]

# BAD
if ( is_true ) message("Hello!")
species[ "tiger" ,]

Curly Braces

# GOOD 
if (is_true) {
  # do something
}

if (is_true) {
  # do something
} else {
  # do something else
}

# BAD
if (is_true)
{
  # do something
}

if (is_true) { # do something }
else { # do something else }
# GOOD 
if (is_true) {
  # do something
} else {
  # do something else
}

# BAD
if (is_true) {
  # do something
}
else {
  # do something else 
}
# GOOD 
if (is_true) {
  # do something
  # and then something else
}

# BAD
if (is_true) {
# do something
# and then something else
}
# Good 
if (is_true) return(value)

Indentation

New Line

# GOOD
long_function_name <- function(arg1, arg2, arg3, arg4, 
                               long_argument_name1 = TRUE)

plot(table(rpois(100, 5)), type = "h", col = "red", lwd = 10, 
     main = "rpois(100, lambda = 5)")
# GOOD
long_function_name <- function(long_argument_name1 = c("value1", "value2"),
                               long_argument_name2 = TRUE,
                               long_argument_name3 = NULL,
                               long_argument_name4 = FALSE)

list(
  mean = mean(x),
  sd = sd(x),
  var = var(x),
  min = min(x),
  max = max(x),
  median = median(x)
)
# GOOD
if (some_very_long_name_1 == 1 &&
    some_very_long_name_2 == 1 ||
    some_very_long_name_3 %in% some_very_long_name_4)

# BAD
if (some_very_long_name_1 == 1
    && some_very_long_name_2 == 1
    || some_very_long_name_3 %in% some_very_long_name_4)
# GOOD 
normal_pdf <- 1 / sqrt(2 * pi * d_sigma ^ 2) *
  exp(-(x - d_mean) ^ 2 / 2 / s ^ 2)

# BAD
normal_pdf <- 1 / sqrt(2 * pi * d_sigma ^ 2)
  * exp(-(x - d_mean) ^ 2 / 2 / d_sigma ^ 2)

Comments

# This is a comment.
# GOOD
# define iterator
i <- 1

# BAD
# set i to 1
i <- 1
plot(price, weight) # Plot a scatter chart of price and weight
# Read data --------------------------------------------------------------------

# Clean data -------------------------------------------------------------------

This coding style guide is adapted from the the tidyverse style guide, the rOpenSci Packages book and Iegor Rudnytskyi's R Coding Style Guide.



crp2a/gammaShiny documentation built on Jan. 14, 2025, 10:56 p.m.