[R] Call R from Matlab

Julian Keil julian.keil at gmail.com
Fri Feb 27 16:28:39 CET 2015


Dear all,

I have a Mac, OSX 10.7.5, run R 3.1.2 (2014-10-31) and Matlab R2013a (8.1.0.604).

I try to call R from within Matlab to run a function in Batch mode.
To do this, I followed this example:

From: www.mathworks.com/matlabcentral/newsreader/view_thread/163726

In an m-file you would have:
~~~~~~~~~~~~~~~~~~~~~
data = sum(rand(100), 1);
csvwrite('data.csv', data(:)); % Write as a column
system('R CMD BATCH calc.R outputForDebugging.txt');
testResults = csvread('testResults.csv');
testResultsStruct = struct('W_statistic', testResults(1), 'p_value', testResults(2))
~~~~~~~~~~~~~~~~~~~~~

In the calc.R r-file you would have:
~~~~~~~~~~~~~~~~~~~~~
data <- read.table("data.csv", header = FALSE, sep = ",")
results <- shapiro.test(data$V1)
results2 <- c( results$statistic[["W"]], results$p.value )
write.table(results2, file="testResults.csv", sep = ",", col.names = FALSE, row.names = FALSE, qmethod = "double")
~~~~~~~~~~~~~~~~~~~~~

Now, the R-Part in itself works fine. If I open R and run the calc.R-Script everything works. If I run the calc.R in Batch mode from the terminal, everything works.
However, if I call R from within Matlab using the system command, I get the error:

Error in dyn.load(file, DLLpath = DLLpath, ...) :
  kann shared object '/Library/Frameworks/R.framework/Versions/3.1/Resources/library/stats/libs/stats.so' nicht laden:
  dlopen(/Library/Frameworks/R.framework/Versions/3.1/Resources/library/stats/libs/stats.so, 6): Symbol not found: __gfortran_pow_r8_i4
  Referenced from: /Library/Frameworks/R.framework/Versions/3.1/Resources/lib/libRlapack.dylib
  Expected in: /Applications/MATLAB_R2013a.app/sys/os/maci64/libgfortran.2.dylib
 in /Library/Frameworks/R.framework/Versions/3.1/Resources/lib/libRlapack.dylib
Beim Start - Warnmeldung:
package 'stats' in options("defaultPackages") was not found

It appears that R fails to load some objects. However, I don't understand why this only happens if I call R from Matlab, not when I start R by itself or use the terminal to start R.
I already installed R new, but it did not help.

Can anyone enlighten my on this matter?

Thanks a lot!

Julian


********************
Dr. Julian Keil

AG Multisensorische Integration
Psychiatrische Universitätsklinik
der Charité im St. Hedwig-Krankenhaus
Große Hamburger Straße 5-11, Raum E 307
10115 Berlin

Telefon: +49-30-2311-1879
Fax:        +49-30-2311-2209
http://psy-ccm.charite.de/forschung/bildgebung/ag_multisensorische_integration

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 495 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20150227/b78a5706/attachment.bin>


More information about the R-help mailing list