Microsoft have recently added support for R to their Visual Studio 2015 development environment. R is a programming language for statistical and graphical programming that is widely used by statisticians and data miners.
To use R with Visual Studio, you need to download and install R Tools for Visual Studio and Microsoft R Open. These will work fine with the Community Edition of Visual Studio 2015. In addition to support for R, R Tools for Visual Studio adds a debugger and Intellisense, as well as support for multi-threading and cluster computing.
It would be useful if R was able to interface with other active applications, for example with COM. Unfortunately at the moment, R Tools for Visual Studio does not add any ability in this area. There is the RDCOMClient package from Omegahat. Unfortunately the documentation and installation instructions are old, incomplete, or simply wrong. The source code is also available, but I could not get it to build.
I did manage to eventually get it to install with R Tools for Visual Studio, using the following two lines:
Very simple! Omegahat’s own installation instructions do not work because they refer to their own repository. However, the standard CRAN repository works just fine.
Here are some usage examples, working with Excel:
# Create/Connect to the Excel.Application COM server
excel <- COMCreate("Excel.Application")
# Properties are accessed using [[ ]] notation
excel[["Visible"]] <- TRUE
# Methods are called using the $fn( ) notation
workbook <- excel[["Workbooks"]]$Add(1)
worksheet <- workbook[["Worksheets"]]$Add()
worksheet[["Name"]] <- "R Generated Sheet"
# Fetch a cell reference, and write a string to it
cell11 <- worksheet$Range("A1")
cell11[["Value"]] <- "Hello"
# Create some test data
data_vector <- c(1, 2, 3, 4, 5, 6)
data_matrix <- matrix(1:20,byrow=TRUE, nrow=5, ncol=4)
# Vectors are written as columns
# The following fills two columns with the vector data_vector
rv <- worksheet$Range(worksheet$Cells(2, 1), worksheet$Cells(7, 2))
rv[["Value"]] <- asCOMArray(data_vector)
# The following writes a 2d matrix
rm <- worksheet$Range(worksheet$Cells(2, 5+0), worksheet$Cells(6, 5+3))
rm[["Value"]] <- asCOMArray(data_matrix)
# Fetching a single value
rngv <- worksheet$Cells(3, 6)
val1 <- rngv[["Value"]]
print("Value of cell 3,6:")
# Fetching a matrix: note we need to reformat the results into the required shape
rngmm <- worksheet$Range(worksheet$Cells(2, 5 + 0), worksheet$Cells(6, 5 + 3))
mt_raw <- rngmm[["Value"]]
mt <- matrix(unlist(mt),ncol=4, byrow = TRUE)
The excel.link package is available specifically for Excel communication, and should be more convenient. However, Excel makes a useful demonstration of RDCOMClient’s COM interface.
The following documentation is also available:
- Using COM facilities in R; A tutorial from the Dept. of Statistics at UC Davis.
- Functions from RDCOMClient package; RDRR.IO R Documentation Repository
The RDCOMServer package can be used if you wish to create COM server objects within R. External applications can then call these. For example, you might want to encapsulate a data analysis routine in a COM server that other applications can then call.
The article, Statistically Valid Interpolation with R, uses RDCOMClient along with the sp and gstat packages to krige input point data (Ogwalla aquifer depths) in R and then use it to create a gridded layer in Caliper Maptitude.