Friday, May 07, 2010

How to run R code in Matlab

R (site here) is a great open-source environment for statistical analysis. But I'm a Matlab user. Luckily, it is pretty easy to run R code from Matlab. Since I just set it up in my Matlab environment, I thought I'd write out the recipe I followed. I have only done the following in Windows XP, and I used Matlab version 7.8. I think it will only work in Windows. It assumes you already have R and Matlab properly installed on your computer.

Of course, this doesn't mean I don't have to learn how to use R, it just means I get to do it all in Matlab (and note for fellow Matlab users, there is a great cheat sheet that shows how to translate between the two).

1. Install the R package rscproxy.
In R, enter:
>install.packages("rscproxy")
to install the package.

2. Install the R(D)Com server.
Download it here. The server allows Matlab to talk with R. I installed it using the default settings without checking or unchecking any boxes. Note this server is built for Scilab, which is an open source version of Matlab, but it seems to work for Matlab too.

3. Download the Matlab R-Link toolbox
Get MATLAB_RLINK.zip here, unzip the contents, and paste MATLAB_RLINK in Matlab's toolbox folder (or whatever folder you want). Be sure to add MATLAB_RLINK to your Matlab path.

4. Restart your computer.

5. Is it working?
To see if the toolbox is working, start Matlab and enter 'Rdemo' at the command prompt. This should evoke:
b =
1 4 9 16 25 36 49 64 81 100

c =
2 5 10 17 26 37 50 65 82 101

6. Have fun!
If Rdemo worked, you are ready to go!

For instance, enter the following in Matlab:
openR; %Open connection to R server
x=[1:50]; %create x values in Matlab
putRdata('x',x); %put data into R workspace
evalR('y<-sqrt(x)'); %evaluate in R
evalR('plot(x,y)') %plot in R
To close the connection to R, and the graphs opened from R, enter:
closeR;

7. Problems?
If the above doesn't work, go to C:\Program Files\R, open the (D)COM Server folder, go to 'bin', copy 'sciproxy.dll', and paste it in C:\Program Files\MATLAB\R2009a\bin (obviously you may have a different path to Matlab's binary folder). Close Matlab, and restart your computer.

If that doesn't help, I probably won't be able to help, but go ahead and ask as someone might know. The site where you downloaded R-Matlab has some useful Q&A so you might inquire there.

8. Acknowledgments
This is basically an updated version of Kevin Murphy's site. Please let me know if anything here becomes obsolete.

9. Caveat (added 6/18/12)

From the comments section:
After using R(D)COM and Matlab R-link for a while, I do not recommend it. The COM interface has trouble parsing many commands and it is difficult to debug the code. I recommend using a system command from Matlab as described in the R Wiki. This also avoids having to install all of the RAndFriends programs. 

26 comments:

Jim said...

Thank you, this was very helpful.

Eric Thomson said...

I'm glad it helped!

Sully said...

Many thanks for this write up, it was very helpful.

One problem I ran into was that I had R installed on a server, so it never got into my PC's registry since I did not install it on the PC, and the R(D)COM server needs a registry entry to find R. Luckily R has a tool to put its location into the registry. It is in the bin directory of R and called RSetReg.exe. I ran that and then the R(D)COM server was able to find and start the R server.

Eric Thomson said...

Thanks Sully, that's really helpful to know.

Anonymous said...

Thank you very much! That's very helpful! I just want to said that there is a problem with the last R version (2.12.0): it doesn't work! The DCOM can't make the connexion. I still had R version 2.9.2 and by re-installing this old version it works. I don't know at which version the problem occurs, but I think it could be useful to know...

Eric Thomson said...

Anonymous: thanks for that tip I sure hope they fix the problem soon!

RBCCE said...

Thanks for nice instruccions, installed for R 2.13 (32b) and Matlab 2010b. As sugested in matlab Central ( http://www.mathworks.com/matlabcentral/fileexchange/5051), I use the Rcom from http://rcom.univie.ac.at/download/current/statconnDCOM.latest.exe (ver 3.1-2B7) and it works.

Eric Thomson said...

RBCCE thanks for the update good to know it is still working.

Cagri said...

Thank you for the instructions. They work and I can call R from Matlab, as long as what I'm calling is not a function written in R.

However, if I write a function in R, and if I call this function from Matlab, then I get the following error message: Problem evaluating command source("square.R").
Invoke Error, Dispatch Exception: Object is static; operation not allowed.

Any suggestions on how to solve it are welcome.

Albina said...

Thank you and thanks also for RBCCE, with your tip it works!!!

Rising.Star said...

I have the same Problem here as Cagri, hope someone could help to answer

Anonymous said...

Any one successively link R in matlab in linux? I tred to use JRI(java-R interfact) to link them but failed.

Jeff Parker said...

Thanks, this was very helpful! I downloaded the package RAndFriends from here, which installs an up-to-date version of R and rscproxy. Then I downloaded an up-to-date version of statconnDCOM from the same page (at the time of writing, version 3.3-0B2). It worked perfectly on Windows 7!

Anonymous said...

Very nice instructions! For me (Windows 7) worked the RAndFriends installation

Eric Thomson said...

Thanks for letting me know. It is useful to know the instructions still work!

Cary said...

After using R(D)COM and Matlab R-link for a while, I do not recommend it. The COM interface has trouble parsing many commands and it is difficult to debug the code. I recommend using a system command from Matlab as described in the R Wiki. This also avoids having to install all of the RAndFriends programs.

Eric Thomson said...

Thanks Cary I will look into that, in the meantime I'm posting your comment in the main body of this post.

Unknown said...

Thanks, very helpful!
sometimes there is something wrong with the R(D)com server.
But the latest one is mostly like running well.

http://rcom.univie.ac.at/download/current/statconnDCOM.latest.exe (ver 3.1-2B7)

Eric Thomson said...

Thanks Chunguo: I will put a permanent link to that...

Hasan said...

Thank you Eric. It works very well :)

Michael said...

Hi Eric,

Just starting to use R being a long time Matlab user. R has great statistics package but prefer to use Matlab for all its features. I just tried this out with R 3.1.2 and works great. I had installed the RExcel so already had the R(D)COM and rscproxy installed. No problems at all in Matlab. Thanks for the information.

MaggieZhao said...

It is great help! Thanks!

Roland said...

Hi Eric,
Thanks! It works real good with R 32Bit.
Is there the possibility to work with R 64Bit as well?

Eric Thomson said...

Roland I am not sure --- if anyone knows please let us know.

Unknown said...

Hi,
i need your help. i have the version of Matlab R2015a and R 3.3.2 64 bits installed in my computer. i instaled the DCOM and i followed the different steps in the followed link http://neurochannels.blogspot.com/2010/05/how-to-run-r-code-in-matlab.html. But the Rdemo function didn't run. please advise me what can i do in this case to run this function . i wait your feedback.

Eric Thomson said...

Saoussen I am not sure it will work in 64 bit, frankly. If desperate you might need to install a 32 bit version of Matlab and see if it works. But there may be better options out there. I have not done this in a while it seems these instructions may be growing obsolete...