In quantitative finance both R and Excel are the basis tools for any type of analysis. Whenever one has to use Excel in conjunction with R, there are many ways to approach the problem and many solutions. It depends on what you really want to do and the size of the dataset you’re dealing with. I list some possible connections in the table below.
|I want to…||R function/package|
|Read Excel spreadsheet in R||gdata
|Read R output in Excel||write.table
|Execute R code in VBA||Custom function
|Execute R code from Excel spreadsheet||RExcel|
|Execute VBA code in R||Custom function|
|Fully integrate R and Excel||RExcel|
1 – Read Excel spreadsheet in R
- gdata: it requires you to install additional Perl libraries on Windows platforms but it’s very powerful.
require(gdata) myDf <- read.xls ("myfile.xlsx"), sheet = 1, header = TRUE)
- RODBC: This is reported for completeness only. It’s rather dated; there are better ways to interact with Excel nowadays.
- XLConnect: It might be slow for large dataset but very powerful otherwise.
require(XLConnect) wb <- loadWorkbook("myfile.xlsx") myDf <- readWorksheet(wb, sheet = "Sheet1", header = TRUE)
- xlsx: Prefer the
read.xlsx2()over read.xlsx(), it’s significantly faster for large dataset.
require(xlsx) read.xlsx2("myfile.xlsx", sheetName = "Sheet1")
- xlsReadWrite: Available for Windows only. It’s rather fast but doesn’t support .xlsx files which is a serious drawback. It has been removed from CRAN lately.
- read.table(“clipboard”): It allows to copy data from Excel and read it directly in R. This is the quick and dirty R/Excel interaction but it’s very useful in some cases.
myDf <- read.table("clipboard")
2 – Read R output in Excel
First create a csv output from an R data.frame then read this file in Excel. There is one function that you need to know it’s write.table. You might also want to consider: write.csv which uses “.” for the decimal point and a comma for the separator and write.csv2 which uses a comma for the decimal point and a semicolon for the separator.
x <- cbind(rnorm(20),runif(20)) colnames(x) <- c("A","B") write.table(x,"your_path",sep=",",row.names=FALSE)
3 – Execute R code in VBA
RExcel is from my perspective the best suited tool but there is at least one alternative. You can run a batch file within the VBA code. If R.exe is in your PATH, the general syntax for the batch file (.bat) is:
R CMD BATCH [options] myRScript.R
Here’s an example of how to integrate the batch file above within your VBA code.
4 - Execute R code from an Excel spreadsheet
Rexcel is the only tool I know for the task. Generally speaking once you installed RExcel you insert the excel code within a cell and execute from RExcel spreadsheet menu. See the RExcel references below for an example.
5 – Execute VBA code in R
This is something I came across but I never tested it myself. This is a two steps process. First write a VBscript wrapper that calls the VBA code. Second run the VBscript in R with the
shell functions. The method is described in full details here.
6 – Fully integrate R and Excel
RExcel is a project developped by Thomas Baier and Erich Neuwirth, “making R accessible from Excel and allowing to use Excel as a frontend to R”. It allows communication in both directions: Excel to R and R to Excel and covers most of what is described above and more. I’m not going to put any example of RExcel use here as the topic is largely covered elsewhere but I will show you where to find the relevant information. There is a wiki for installing RExcel and an excellent tutorial available here. I also recommand the following two documents: RExcel – Using R from within Excel and High-Level Interface Between R and Excel. They both give an in-depth view of RExcel capabilities.
The list above is probably not exhaustive. Feel free to come back to me for any addition or modification you might find useful. All code snipets have been created by Pretty R at inside-R.org