[Rd] Problem in calling R functions from Matlab
Vandita Srivastava
vandita at iirs.gov.in
Fri Aug 26 14:44:50 CEST 2011
Hi,
I wish to use R (version 2.13.1) from within Matlab(ver R2009a) on windows XP plaform (on both 64 bit and 32 bit OS) . For this I have installed StatConnector (http://rcom.univie.ac.at/download/current/statconnDCOM.latest.exe) for calling R from within Matlab (R2009a) on Windows XP platform. I have added all the files of MATLAB_RLINK folder (downloaded from http://www.mathworks.com/matlabcentral/fileexchange/5051) into the default Matlab working path (D:\User\MATLAB) and also added the folder MATLAB_RLINK to the matlab path C:\Program Files\MATLAB\R2009a\toolbox . It seems that Matlab is able to connect to R however I am facing a problem while trying to call R functions from within Matlab. I have loaded all required libraries. Shown below is the sequence of commands tried in blue, outcomes in black and errors/unexpected/ undesired outcomes in red colour, and my comments in black,italic after the command:
>> [a b c] = openR
a = 1
b = ''
c = COM.StatConnectorSrv_StatConnector
>> Rdemo
b = 1 4 9 16 25 36 49 64 81 100
c = 2 5 10 17 26 37 50 65 82 101
I then tried running the Rdemo available on http://www.mathworks.com/matlabcentral/fx_files/5051/1/content/Rdemo.html
evalR('demo("persp")') works well. Also the arithmetic functions all work well:
a = 1:10;
putRdata('a',a)
b = evalR('a^2')
evalR('b <- a^2');
evalR('c <- b + 1');
c = getRdata('c')
However ‘Now copy the volcano data into MATLAB” section and all related sessions therefore don’t work.
>> volcano = getRdata('volcano')
volcano = []
>> size(volcano)
ans = 0 0
>> surf(volcano);
(see the attached figure:surf_volcano_output.jpg)
It seems Matlab is able to read/get data from R. Other subsequent commands related to this section don’t work.
I tried looking at the loaded libraries in R and in Matlab. In R everything works well, but doesn’t seem to be so in Matlab:
>> [a b c] = evalR('.libPaths()')
a = C:/Program Files/R/R-2.13.1/library
b = 1
c = ''
>> [a b c] = evalR('library()')
a = 'ActiveX VT_ERROR: '
'base'
'ActiveX VT_ERROR: '
b = 1
c = ''
>> [a b c] = evalR('library(rgdal)')
a = 'rscproxy'
'lattice'
'fields'
'spam'
'rgdal'
'sp'
'stats'
'graphics'
'grDevices'
'utils'
'datasets'
'methods'
'base'
b = 1
c = ''
As it shows that rgdal library is loaded, also the rscproxy library is loaded, in addition to all others. I tried following:
imshow(imread('testimg_p.tif')) displays the image properly, however calling this from R using rgdal from within Matlab gives error;
>> [a b c] = evalR('img <- readGDAL("testimg_p.tif")')
a = []
b = 0
c = Invoke Error, Dispatch Exception: Object is static; operation not allowed
The command works from R prompt:
> img <- readGDAL("testimg_p.tif")
testimg_p.tif has GDAL driver GTiff
and has 280 rows and 272 columns
It is giving same error while loading gstat library:
>> [a b c] = evalR('library(gstat) ')
a = []
b = 0
c = Invoke Error, Dispatch Exception: Object is static; operation not allowed
I guess the error is not related to readGDAL or gstat library but something else which I am unable to trace. I have also tried to explore if this is problem related to version of Matlab/windows 64/32 bit or R version but even that does not seem to be an issue. (I have tried this on two differemt machines one Workstation with WinXP 64 bit OS with R version 2.13.1and Matlab Version 7.8.0 R2009a and also on another portable workstation with WinXP 32 bit OS with R version 2.12.1 and Matlab Version 7.8.0 R2009a. This indicates that version of R or Matlab or win version 64 bit/32 bit does not seem to be an issue.
I also checked for some solution on page http://www.mathworks.com/matlabcentral/fileexchange/5051 where others also faced somewhat similar problem and it was suggested to check for setting R_HOME and PATH variables in the environment variables for your system. I understand I had followed all instructions carefully and hopefully all paths/R_HOME were set correctly, as the command evalR('.libPaths()') recognized R home path correctly.
I request R team to help.
Vandita Srivastava
Scientist/Engineer "SE",
Indian Institute of Remote Sensing(NRSC),
ISRO/Department of Space, Govt. of India
>>Please don't print this Email unless you really need to - this will preserve trees
on planet earth.
More information about the R-devel
mailing list