### Table of Contents

# R(D)COM, rcom, and other software systems

## Mathematica

#### Using R(D)Com and R(Com) with Mathematica

It is very easy to use R from within Mathematica on Windows

Here is an example.

Needs["NETLink`"] myR = CreateCOMObject["RCOMServerLib.StatConnector"] myR@Init["R"] myR@SetSymbol["xxx",12321] result1=myR@GetSymbol["xxx"] myR@EvaluateNoReturn["randmat<-matrix(rnorm(100),10)"] rmat=myR@GetSymbol["randmat"] result2=myR@Evaluate["solve(matrix(1:4,2))"] myR@SetSymbol["myArr",Array[#1*#2*#3 &, {2, 3, 4}]]

This example needs RGui (or RTerm) started and library rcom loaded. You can switch between R and Mathematica and work with both interactively.

To use the (hidden) stconnDCOM server, you have to replace the line

myR = CreateCOMObject["RCOMServerLib.StatConnector"]

by

myR = CreateCOMObject["{18C8B662-81A2-11D3-9254-00E09812F727}"]

## MATLAB

#### Using R(D)Com and R(Com) with MATLAB

- I assume you followed the R(D)Com installation instructions and installed it correctly.

##### A simple sample session that illustrates the available commands

- Start MATLAB (R does not have to be started). In the following, » is the Matlab prompt; it does not have to be typed.
- »R_handle=actxserver('StatConnectorSrv.StatConnector'); % open the actx server in matlab, assign handle to R
- »R_handle.Init('R'); % initiate R, make sure R is on the path; there is now an open channel to R
- »R_handle.SetSymbol('x',1:10) %Assigns the MATLAB vector [1 2 …..10] to the 1 by 10 <b>array</b> x in R
- »R_handle.SetSymbol('y',5) %Assigns the MATLAB scalar 5 to the variable y in R
- »ReturnVal=R_handle.Evaluate('x+y') %R calculates x+y and returns a value, this is assigned to ReturnVal in Matlab.
- » %This command will crash if R doesn't return a valid value for whatever reason.
- »R_handle.EvaluateNoReturn('z ← x+y') % This command is safer. R doesnt return a value, instead x+y is assigned to z within R
- »MatlabZ=R_handle.GetSymbol('z') % This command gets the value of z in R (i.e. x+y) and assigns it to MatlabZ in Matlab
- »R_handle.Close % We are done using r, close the connection

##### Possible problems, and debugging hints

- There is a potentially confusing point: .SetSymbol transfers MATLAB vectors (x=1:10;) to R 1 by n matrices. This can cause trouble when using say lm and expecting it to behave as if x and y were vectors; see this thread in the rcom-l archives. This happens because lm performs multivariate regression when it sees matrix inputs, as explained in this message. To get lm to work, one has to explicitly typecast x and y to be vectors using R_handle.EvaluateNoReturn('x=as.vector(x)').
- Also, note that “R(D)COM and rcom can only return scalar values (e.g. strings) and arrays. Many R functions have R lists as return values. Lists cannot be transferred by R(D)COM and rcom. The solution is to transfer only components of lists which in many cases are arrays.”
- Example:
- .Evaluate ( z ← lm(y~x) ) will crash because z is a list and cannot be transferred. So use
- .EvaluateNoReturn( z ← lm(y~x) )
- .GetSymbol( coef(z)[1] ) to get the intercept and .GetSymbol( coef(z) [2] ) to get the slope. or alternatively, use .GetSymbol( as.matrix ( coef(z) ) ) to get both values at once.

##### How to debug

- Debugging when using RDCom can be extremely difficult because one does not have access to the internals of R, except through the RDCom interface from Matlab. There is no way to see what R is doing directly.
- This is where rcom comes in extremely handy (for general considerations of when to use rdcom and when to use rcom, see: http://mailman.csd.univie.ac.at/pipermail/rcom-l/2005-October/000764.html.
- Using rcom instead of RDCom is extremely easy: start R, type library(rcom) at the R-prompt after installing the rcom library from CRAN.
- Then in Matlab, you can repeat the entire session above exactly as before,
that you replace**except**- »R_handle=actxserver('StatConnectorSrv.StatConnector');
*with* - »R_handle=actxserver('RCOMServerLib.StatConnector'); % open the actx server in matlab, assign handle to R

- The main difference is that now, variables like x, y and z can be examined in the R workspace (from the R window) and computations can be performed both directly in R and via the rcom COM interface from MATLAB to see why things are not going as expected.

##### Notes

- You might find Robert Henson's library R-link at MATLAB Central (a little) more user-friendly to use. It is simply a wrapper for the functions discussed above

### Still unresolved

- R_handle.EvaluateNoReturn('x=3;y=4;') only seems to evalulate the first statement. So in the R workspace, x is created with the value 3, but y is not created. Is there a way to get R_handle.EvaluateNoReturn to evaluate multiple statements in the same call ?

Possibly R_handle.EvaluateNoReturn('x=3\ny=4;') does the trick.