- Solve real problems with our hands-on interface
- Progress from basic puts and calls to advanced strategies
We take a look back at this classic piece from The R Trader on practical tips for Naming conventions, Files organisation, Syntax and Miscellaneous items.
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 = “https://www.datatailor.be/rcube”, type = “source”)
3 – Syntax
4 – Miscellaneous
This post has been written using my own experience and the following documents:
Visit the The R Trader Blog https://www.thertrader.com to get additional tips and samples.
Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.
This material is from The R Trader and is being posted with its permission. The views expressed in this material are solely those of the author and/or The R Trader and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.
Join The Conversation
For specific platform feedback and suggestions, please submit it directly to our team using these instructions.
If you have an account-specific question or concern, please reach out to Client Services.
We encourage you to look through our FAQs before posting. Your question may already be covered!