This style guide is based on Henrik Bengtsson's "R Coding Conventions", Google's "R Style Guide" and Hadley Wickham's "Advanced R" style guide. All R code that is part of the micEcon project should follow these coding conventions, unless there is good reason to do otherwise (e.g. much better readability for humans).
Comments and suggestions regarding this style guide are highly welcome. Please post your feedback via the "Open-Discussion" forum at the R-Forge site of the micEcon project.
File names should be meaningful,
start with a lower-case letter,
continue in camelCase,
contain no blanks or symbols,
and end in .R
.
# Good fitModels.R utilityFunctions.R # Bad my_functions.r all-stuff.r
Names of variables, functions and arguments should be meaningful and concise,
start with a lower-case letter,
continue in camelCase,
and not contain symbols such as underscores, hyphens, or dots.
Meaningful names of variables are usually nouns and
meaningful names of functions usually contain verbs.
Names of boolean variables could start with is
.
It often takes some time to find concise and meaningful names—take
the time!
# Good dayOne fitModel( formula ) isFinished <- TRUE # Bad first_day_of_the_month day.one DayOne dayone d1 fin <- TRUE isNotFinished <- FALSE fit_model( x ) model.result( estimatedModelSpec )
Where possible, avoid using names of existing functions and variables, because this will cause confusion for the readers and users of your code.
# Bad T <- FALSE c <- 10 mean <- function(x) sum(x)
Place spaces around operators
(+
, -
, *
, /
,
^
, <-
, etc.).
Also place spaces also around =
in function calls.
# Good totalInches <- 12 * feet + inches plot( a + b, type = "l" ) # Bad totalInches<-12*feet+inches plot( a+b, type="l" )
There's a small exception to this rule:
:
, ::
and :::
do not need spaces around them.
# Good x <- 1:10 base::get # Bad x <- 1 : 10 base :: get
Never place a space before left (opening) parentheses/brackets. Place a space after left (opening) parenthesis/brackets and before right (closing) parentheses/brackets, unless the part within the parentheses/brackets is small. Always put a space after a comma, and usually not before a comma (just like in regular English).
# Good myVector[3] myMatrix[ 2, 5 ] total <- sum( x[ , 1 ] ) total <- sum( x[ 1, ] ) if( TRUE ) if(TRUE) plot(x) # Bad myVector [3] myMatrix[ 2 ,5 ] total <- sum(x[ ,1 ]) total <- sum(x[1,]) if (TRUE) plot (x )
Extra spacing (i.e., more than one space in a row) is ok
if it improves alignment of equal signs or assignments
(<-
).
# Good list( total = a + b + c, mean = ( a + b + c ) / n )
An opening curly brace should never go on its own line
and should always be followed by a new line.
A closing curly brace should always go on its own line,
unless it's followed by else
.
Always surround the command else
by braces.
The code inside curly braces should be indented.
# Good if( y < 0 && debug ) { message( "Y is negative" ) } if( y == 0 ) { log(x) } else { y ^ x } # Bad if( y < 0 && debug ) message( "Y is negative" ) if( y < 0 && debug ) message( "Y is negative" ) if( y == 0 ) { log(x) } else { y ^ x }
Strive to limit your code to 80 characters per line.
This fits comfortably on a printed page with a reasonably sized font
and facilitates comparisons of different versions of the files
(e.g. when using version control systems
such as git
or subversion
).
If you find yourself running out of room,
this is a good indication that you should encapsulate some of the work
in a separate function.
If you use RStudio, you can enable the option "Show margin"
and set the option "Margin column" to "80".
When indenting your code, use two spaces. Never use tabs or mix tabs and spaces. If you use RStudio, you should disable the option "Vertically align arguments in auto indent".
# Good longObjectName <- longFunctionName( x = "a long argument", y = "another argument" z = "another long argument" ) # Bad longObjectName <- longFunctionName( x = "a long argument", y = "another argument" z = "another long argument" ) longObjectName <- longFunctionName( x = "a long argument", y = "another argument" z = "another long argument" )
# Good predictWeather <- function( location, period, frequency = "hourly", showUncertainty = FALSE) # Bad predictWeather <- function( location, frequency = "hourly", period, showUncertainty = FALSE ) predictWeather <- function( location, period, frequency = "hourly", showUncertainty = FALSE)
stop()
.
Potential problems should be raised
with meaningful and helpful warning messages
using warning()
.
Use <-
, not =
, for assignment.
# Good x <- 5 # Bad x = 5
attach()
are numerous.
Do not use it.
# Good mean( myData$x1 + myData$x2 + myData$x3 ) mean( with( myData, x1 + x2 + x3 )) # Bad attach( myData ) mean( x1 + x2 + x3 )
# Bad x <- 5; y <- 6; z <- 8
# Good # correlation between weight and height corWeightHeight <- cor( myData$weight, myData$height, use = "complete.obs", # remove observations with NAs method = "pearson" ) # Bad #correlation between weight and height corWeightHeight <- cor( myData$weight, myData$height, use = "complete.obs",# remove observations with NAs method = "pearson" )