Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rJava loading in R(3.4.1) with OS Sierra

Tags:

java

macos

r

I have been around a problem with rJava in the newest version of R(3.4.1), Mac (10.12.5) and Java VM (1.8.0_131, x86_64:)

Problem:

I try it to follow this guide

scottdhoover.wordpress.com/2013/03/05/a-basic-rjava-example/

To be able to run rJava in R.

Some code from r:

library(rJava) .jinit("") .jclassPath() [1] "/Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava/java"

s <- .jnew("java/lang/String", "Hello World!") .jcall(s, "I", "length") [1] 12

So is actually working.. but when I try with my files:

.jaddClassPath("/Users/Camilo/Desktop/20170711_Java2R") .jclassPath() [1] "/Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava/java" "/Users/Camilo/Desktop/20170711_Java2R"

myExchange <- .jnew("myExchange") Error in .jnew("myExchange") : java.lang.ClassNotFoundException

where the file myExchange.java is the same that in the guide and is in the folder in the Desktop.

What I have done:

Basically I tried to instal rJava from R and didn't work. Then I have follow two guides to be able to load the package rJava in R from source

I have follow this guide:

https://github.com/snowflakedb/dplyr-snowflakedb/wiki/Configuring-R-rJava-RJDBC-on-Mac-OS-X (To be able to load rJava)

Where both

   R CMD javareconf
   /usr/libexec/java_home -V

Looks exactly as it should be.

To get rid of the OpenMP problem that people normally have I follow this guide:

http://thecoatlessprofessor.com/programming/openmp-in-r-on-os-x/#clang-before-3-4-0 (To get rid with the OpenMP problem that all people have).

and Finally I create a symlink using this code

  sudo ln -f -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib

and I was able to install the library from R.

But! When I try it from terminal I get this error

   ld: library not found for -lomp
   clang-4.0: error: linker command failed with exit code 1 (use -v to see    invocation)
   make[2]: *** [libjri.jnilib] Error 1
   make[1]: *** [src/JRI.jar] Error 2
   make: *** [jri] Error 2
   ERROR: compilation failed for package ‘rJava’

So I tought: A library or a symlink must to be missing too.. but there is not such a file as in llvm ( Have been solve in llvm with ld: library not found for -lomp )

I know that the error "ClassNotFoundExceptions" could be related with the PATH but It should be OK cos has been declared at the beginning.

btw, I also check the .R/Makevars file that looks like this

  CC=/usr/local/clang4/bin/clang    
  CXX=/usr/local/clang4/bin/clang++
  LDFLAGS=-L/usr/local/clang4/lib

Any ideas how to solve it? Thanks in advance! I hope that is clear.

like image 734
Chino Chiang Avatar asked Jul 12 '17 07:07

Chino Chiang


2 Answers

For me the solution was to install https://github.com/coatless/r-macos-clang and add link

sudo ln -s /usr/local/clang4/lib/libomp.dylib /usr/local/lib/libomp.dylib

Before R CMD javareconf i unset JAVA_HOME.

like image 150
markush81 Avatar answered Oct 04 '22 16:10

markush81


This is a confusing issue and depends on your R version.

For R 3.4 users on Mac Sierra, ignore all the noises about gcc, clang, llvm. R 3.4 uses clang4. Period. So you should do

  • Install clang4
  • Change .R/Makevars to point to clang4
  • Install Java 8
  • R CMD javareconf
  • To address a linking issue, sudo ln -s /usr/local/clang4/lib/libomp.dylib /usr/local/lib/libomp.dylib

Then you should be able to install rJava successfully.

In addition, rJava won't load properly in older RStudio. So make sure you've upgraded to the latest RStudio.

like image 32
Steve Lihn Avatar answered Oct 04 '22 17:10

Steve Lihn