Site icon R-bloggers

R Code – Best practices

[This article was first published on R – The R Trader, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Nothing is more frustrating than a long piece of code with no standard way of naming elements, presenting code or organizing files. It’s not only unreadable but more importantly not reusable. Unfortunately, unlike other programming languages, R has no widely accepted coding best practices. Instead there has been various attempts to put together a few sets of rules. This post is trying to fill the gap by summarizing and/or extracting what I found relevant in those various attempts. It also includes some tips I came up with after years of using R on a daily basis.

1 – Naming conventions

R has no naming conventions that are generally agreed upon. As a newcomer to R it’s useful to decide which naming convention to adopt.

2 – Files organisation

They way files are organised helps making the code more readable. Similarly, the way the code is organised within a file has a significant impact on readability. Files might also have specific purposes. Some might contain only functions that will be used by other files, some might be used to update packages etc…

2.1 – How to organise the files within a project?
2.2 – How to organise the code within each file?
#############################################################
## Stuff I have to do
## thertrader@gmail.com - Feb 2018
#############################################################
​
##############################
# 0 - Load librairies
##############################
library(zoo)
library(xts)

​############################## 
# 1 - Source file 
##############################
dataPath <- "C:/some_directory/some_sub_directory/" 
dataFile <- "some_functions.R" 
source(paste0(dataPath,dataFile))

##############################
# 2 - Start my code
##############################
myPlot <- plot(data,type="l")
2.3 – Files of functions
#############################################################
## Date Functions 
##
## dd.mm.yyyy.to.date: 22.10.2004 to a date
## yyyy-mm-dd.to.date: 2008-07-22 to a date
## dd-mm-yyyy.to.date: 12-05-2001 to a date
##
## thertrader@gmail.com - Jan 2008... 
############################################################

## 22.10.2004 to a date
dd.mm.yyyy.to.date <- function(theDate) {
 myDate <- as.Date(theDate, format = "%d.%m.%Y")
 return(myDate)
}

## 2008-07-22 to a date
yyyy-mm-dd.to.date <- function(theDate) {
 myDate <- as.Date(theDate, format = "%Y-%m-%d")
 return(myDate)
}

## 12-05-2001 to a date
dd-mm-yyyy.to.date <- function(theDate) {
 myDate <- as.Date(theDate, format = "%m-%d-%Y")
 return(myDate)
}
2.4 – Files with packages and addins
##################################################
## Install Package Automatically - to run after R has been upgraded/installed
##
## thertrader@gmail.com - Aug 2010...
##################################################

install.packages(c(
"data.table",
"DEoptim",
"devtools",
"dplyr",
"DT",
"dygraphs",
"ggplot2",
"xts",
"PerformanceAnalytics"))

install.packages("taskscheduleR", repos = "http://www.datatailor.be/rcube", type = "source")

3 – Syntax

4 – Miscellaneous

This post has been written using my own experience and the following documents:

To leave a comment for the author, please follow the link and comment on their blog: R – The R Trader.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.