RMatlab is a wonderful package I just discovered. It interfaces R and Matlab. I use it to make sexy figure from within Matlab. (The alternative is to export data, import it to R, and make a figure. It's possible, but a big pain.) The package works great, but I found it slightly difficult to get working. Here are the steps I took.
Prerequisites
- A recent version of R; I'm using R 2.9.2 from the R debian repositories.
- A working mex compiler.
- A unix environment.
Installation
Download the file RMatlab_0.2-5.tar.gz from http://www.omegahat.org/RMatlab/
Unzip
dgleich@icme-392-dgleich:~/devextern$ tar xzvf RMatlab_0.2-5.tar.gz
Download apply the provided patch. Get it here:
dgleich@icme-392-dgleich:~/devextern$ cd RMatlab
dgleich@icme-392-dgleich:~/devextern/RMatlab$ patch -p1 < ../RMatlab.patch
patching file src/initializeR.c
patching file src/Makefile.in
Configure — the two settings below are for Ubuntu 8.10 with R 2.9.2; to figure out the paths for yourself, check out the section below.
dgleich@icme-392-dgleich:~/devextern/RMatlab$ export R_HOME=/usr/lib64/R
dgleich@icme-392-dgleich:~/devextern/RMatlab$ export R_INCLUDE_DIR=/usr/share/R/include
dgleich@icme-392-dgleich:~/devextern/RMatlab$ ./configure
Install with R.
dgleich@icme-392-dgleich:~/devextern/RMatlab$ cd ..
dgleich@icme-392-dgleich:~/devextern$ R CMD INSTALL RMatlab
Hopefully, there aren’t any problems.
Running
There are quite a few paths you need to set to get RMatlab working properly. Here is the easiest way to do it.
Use the compiled directory to set all the paths. The following command depends on being in the devextern directory. You’ll need to adjust this path if you call the command from elsewhere.
dgleich@icme-392-dgleich:~/devextern$ source RMatlab/inst/scripts/RMatlab.sh
Run Matlab.
dgleich@icme-392-dgleich:~/devextern$ matlab -nojvm -nodisplay
< M A T L A B (R) >
Copyright 1984-2008 The MathWorks, Inc.
Version 7.7.0.471 (R2008b)
September 17, 2008
To get started, type one of these: helpwin, helpdesk, or demo.
For product information, visit www.mathworks.com.
>> initializeR({'RMatlab'})
>> x=callR('runif',10)
x =
0.5233
0.9075
0.0258
0.0107
0.8787
0.6178
0.3865
0.3492
0.0195
0.2071
My preferred way is to set all the paths myself.
dgleich@icme-392-dgleich:~/devextern$ export R_HOME=/usr/lib64/R
dgleich@icme-392-dgleich:~/devextern$ export R_SHARE_DIR=/usr/share/R/share
dgleich@icme-392-dgleich:~/devextern$ matlab -nojvm -nodisplay
< M A T L A B (R) >
Copyright 1984-2008 The MathWorks, Inc.
Version 7.7.0.471 (R2008b)
September 17, 2008
To get started, type one of these: helpwin, helpdesk, or demo.
For product information, visit www.mathworks.com.
>> addpath('~/R/x86_64-pc-linux-gnu-library/2.9/RMatlab/mex/');
>> initializeR({'RMatlab','--vanilla','--silent'})
>> x=callR('rpois',10,3)
x =
5
1
1
2
2
6
2
2
2
2
Sophisticated users may find it convinent to wrap everything up
Finding R paths
To set R_HOME run
$ locate libR.so
and find an appropriate directory. For example, on my computer:
dgleich@icme-392-dgleich:~/devextern$ locate libR.so
/home/dgleich/devextern/R-2.5.1/lib/libR.so
/home/dgleich/devextern/R-2.5.1/src/main/libR.so
/usr/lib/R/lib/libR.so
This means I have two versions of R installed, but the one to use is the /usr/lib/R for R_HOME.
To set R_INCLUDE_DIR, use locate again.
$ locate Rinternals.h
On my computer:
dgleich@icme-392-dgleich:~/devextern$ locate Rinternals.h
/home/dgleich/devextern/R-2.5.1/include/Rinternals.h
/home/dgleich/devextern/R-2.5.1/src/include/Rinternals.h
/usr/share/R/include/Rinternals.h
So I set R_INCLUDE_DIR to /usr/share/R/include