Style Guide for the micEcon project:

Tools for microeconomic analysis and microeconomic modelling in R

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

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

Object names

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)

Spacing

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
)

Curly braces

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 }

Line length

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".

Indentation

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" )

Function Definitions and Calls

Function definitions should first list arguments without default values, followed by those with default values. In both function definitions and function calls, multiple arguments per line are allowed; line breaks are only allowed between assignments.
# 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)

Errors and warnings

Errors should be raised with meaningful and helpful error messages using stop(). Potential problems should be raised with meaningful and helpful warning messages using warning().

Assignment

Use <-, not =, for assignment.

# Good
x <- 5

# Bad
x = 5

Attach

The possibilities for creating errors when using 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 )

Semicolons

Do not terminate your lines with semicolons or use semicolons to put more than one command on the same line.
# Bad
x <- 5;
y <- 6; z <- 8

Comments

Comment your code. Comments should usually explain the why, not the what. Entire commented lines should begin with # and one space. Short comments can be placed after code preceded by two spaces, #, and then one space.
# 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" )

Valid XHTML 1.0!

Last Update: 19 July 2015