1

I am trying to run an R script from within Matlab, and I get the same error as this poster: Calling R from Matlab

As far as I can tell the situations are the same, except my system is running LinuxMint (release 14 Nadia, kernel 3.5.0-17-generic) and the solution in that post does not work for me. Could anyone make a further suggestion? I'm stumped.

Like the poster in the above thread, my call works from a shell but not from within matlab.

r_script.R contains:

foo <- rnorm( 100 )
cat( sd( foo ), '\n' )
cat( mean( foo ), '\n' )

the error message I get within Matlab:

>> system( 'Rscript /tmp/r_script.R' )
Error in dyn.load(file, DLLpath = DLLpath, ...) : 
  unable to load shared object '/usr/lib/R/library/stats/libs/stats.so':
  /usr/local/MATLAB/R2013a/sys/os/glnxa64/libgfortran.so.3: version `GFORTRAN_1.4' not found (required by /usr/lib/liblapack.so.3)
During startup - Warning message:
package ‘stats’ in options("defaultPackages") was not found 
Error: could not find function "rnorm"
Execution halted

ans =

     1

>> !unset DYLD_LIBRARY_PATH; 
>> system( 'Rscript /tmp/r_script.R' )
Error in dyn.load(file, DLLpath = DLLpath, ...) : 
  unable to load shared object '/usr/lib/R/library/stats/libs/stats.so':
  /usr/local/MATLAB/R2013a/sys/os/glnxa64/libgfortran.so.3: version `GFORTRAN_1.4' not found (required by /usr/lib/liblapack.so.3)
During startup - Warning message:
package ‘stats’ in options("defaultPackages") was not found 
Error: could not find function "rnorm"
Execution halted

ans =

     1
Community
  • 1
  • 1
  • 1
    So ... is '/usr/lib/R/library/stats/libs/stats.so' really where your OS thinks it should be? – IRTFM Aug 01 '13 at 00:54
  • Thanks for your response -- I'm confused by your question though. /usr/lib/R/library/stats/libs/stats.so does exist. How would I determine where my OS thinks it should be? – Timothy W. Hilton Aug 01 '13 at 16:48

2 Answers2

4

OK, the responses from Amro and DWin got me pointed in the right direction and I've solved the problem. The problem was Matlab seems to include its own version of libgfortran.so.3, which must be out of date compared to the version on my system (and expected by R). Many thanks to Amro and DWin for helping me out.

For any poor soul who comes along later:

I was able to determine this by running ldd on /usr/lib/R/library/stats/libs/stats.so from both inside and outside of Matlab.

!ldd /usr/lib/R/library/stats/libs/stats.so (from inside Matlab) returns:

<snip>
libgfortran.so.3 => /usr/local/MATLAB/R2013a/sys/os/glnxa64/libgfortran.so.3 (0x00007faff0bbc000)
</snip>

and ldd /usr/lib/R/library/stats/libs/stats.so (from the shell) returns:

<snip>
libgfortran.so.3 => /usr/lib/x86_64-linux-gnu/libgfortran.so.3 (0x00007f3c9b308000)
</snip>

Adding the system libgfortran.so.3 to the beginning of Matlab's LD_LIBRARY_PATH (not the end) solved the problem. From within Matlab:

>> setenv( 'LD_LIBRARY_PATH', strcat( '/usr/lib/x86_64-linux-gnu:', getenv( 'LD_LIBRARY_PATH' ) ) )
>> !/usr/bin/Rscript /tmp/r_script.R                                                               
0.8317391 
0.09262757 
1

This is probably caused by a version mismatch between libraries used by MATLAB and R. You might be able to fix this by using LD_PRELOAD. Use ldd inside and outside MATLAB to figure out whats causing the problem...

In MATLAB:

>> !ldd Rscript

In the shell:

$ ldd Rscript

btw the Linux equivalent to the existing solution would be:

>> !unset LD_LIBRARY_PATH
>> system('...')

so try that out first..

(Note that I am on a Windows machine, so neither solutions are tested)

Community
  • 1
  • 1
Amro
  • 123,847
  • 25
  • 243
  • 454
  • thanks for your response! No luck, though. !ldd /usr/bin/Rscript (within Matlab) and ldd /usr/bin/Rscript (from the shell) produce the same output except for the hexidecimal addresses. I think it is OK that the hex addresses differ? Unsetting LD_LIBRARY_PATH within Matlab produces the same error as before... – Timothy W. Hilton Aug 01 '13 at 16:50
  • @TimothyW.Hilton: can you show the output of `ldd`? Pay attention to the path of the various shared libraries.. – Amro Aug 01 '13 at 17:23
  • Amro, see below -- the problem was with /usr/lib/R/library/stats/libs/stats.so, not with /usr/bin/Rscript. Your reply got me going in the right direction. Thanks! – Timothy W. Hilton Aug 01 '13 at 17:36