Examples
# Good
<- function() {
fit_model # code
}<- data.frame() results_df
At ESQlabs, we follow the tidyverse style guide with very few changes. The following sections outline the most important differences with the Tidyverse guide. To learn more about original Tidyverse style guide, please refer to the Tidyverse style highlights section.
For simplicity, users should use snake_case
as recommended by the tidyverse style (this is not the case for developers that should follow the OSPS-R coding standards).
However the naming style depends on the type of object:
Variable and function names should use only letters and numbers. Use snake_case
to separate words within a name:
# Good
<- function() {
fit_model # code
}<- data.frame() results_df
True constant variables should use ALL_CAPS
casing:
# Good
<- 1 CONSTANT_VAR
Use short meaningful and understandable names. The code should read as a story and only some well-known abbreviations should be used:
# Good
<- read_excel("pkDataFile.xls")
pk_data
# Bad
<- read_excel("pkDataFile.xls") # Too long pharmacokinetics_data
Because it is not explicitly said in the tidyverse guideline and for improved code readability, use the following indentation settings:
You can check that these settings are correctly set in Tools > Global Options > Code > Editing:
return()
Prefer using return()
for explicitly returning result, although you can rely on R to implicitly return the result of the last evaluated expression in a function.
Avoid having long lines. Restrict yourself to 120 characters (instead of the usual limit of 80 characters).
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.
2.2.4 Comments
#
and a single space.