Examples
# Good
01_preprocessing.R
02_fit_models.R
utility_functions.R
The tidyverse style guide is the reference regarding R coding style. The guide is quite comprehensive, with a lot of examples of what to do and what not to do. If you never read it, you should read it at least once.
Here we highlight the main aspects of the tidyverse style guide, in a very synthetic way. This is to be used more as a checklist. Please refer to the full tidyverse style guide for more details and examples.
Examples listed here might have been adapted, when appropriate, to follow ESQlabs’ style instead of the Tidyverse one.
.R
.-
, and _
.# Good
01_preprocessing.R
02_fit_models.R
utility_functions.R
-
and =
) to break up your file into easily readable chunks.# Load packages -----------------------
library(esqlabsR)
# Load Data ---------------------------
# Plot data ---------------------------
## Plot time profile ------------------
## Plot observed vs simulated ---------
_
to separate words within a name (so called snake case).# Good
# Variable names (nouns)
day_one
day_1
# Function names (verbs)
add_rows()
permute()
# Bad
# too long
first_day_of_the_month # not explicit
d1 row_adder() # not a verb
<- FALSE # reusing of common name
T <- 10 # reusing of common function mean
<-
, not =
, for assignment.+
, -
, /
, *
, <-
, =
, ==
, &
, &&
, |
, ||
, >
, <
, >=
, <=
, %in%
, etc.) should always be surrounded by spaces.^
, :
, $
, []
, [[]]
, ::
, :::
, @
, etc.) should never be surrounded by spaces.<-
or =
.# Good
1]
my_data[, <- 1:10
x <- c(1, 5, 8)
y
<- (feet * 12) + inches
height <- weight^2 / height bmi
=
when used in function calls.()
used for function arguments definition.# Good
mean(x, na.rm = TRUE)
<- function(x, y) {
my_function # As usual code is indented by two spaces
}
()
when used with if
, for
, or while
.else
should be on the same line as }
.&
and |
should never be used inside of an if
clause because as they can return vectors. Always use &&
and ||
instead.# Good
if (y < 0 && debug) {
message("y is negative")
else {
} message("y is positive or zero")
}
<- if (x > 10) "big" else "small" message
{
should always be followed by a new line. Related code (e.g., an if
clause, a function declaration, …) must be on the same line as the opening brace.}
should always go on its own line, unless it’s followed by else
.# Good
if (y == 0) {
if (x > 0) {
log(x)
else {
} message("x is negative or zero")
}else {
} ^x
y }
%>%
should always have a space before it, and should usually be followed by a new line.%>%
iris group_by(Species) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
gather(measure, value, -Species) %>%
arrange(value)
+
should always have a space before it, and should be followed by a new line (such that each layer is on its own line).ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length)) +
geom_point() +
labs(x = "Sepal Width (cm)", y = "Sepal Length (cm)")
)
. This makes the code easier to read and to change later.# Good
do_something_very_complicated(
something = "that",
requires = many,
arguments = "some of which may be long"
)
(
of the function). This is the preferred method.)
and leading {
should go on the same line as the last argument.# Function indent (preferred method)
<- function(a = "a long argument",
long_function_name b = "another argument",
c = "another long argument") {
# As usual code is indented by two spaces
}
# Double indent
<- function(
long_function_name a = "a long argument",
b = "another argument",
c = "another long argument") {
# As usual code is indented by two spaces
}
TRUE
and FALSE
over T
and F
."
, not '
, for quoting text.;
at the end of a line, and don’t use ;
to put multiple commands on one line.
3.4 Comments
#
and a single space.