Run rJava with RStudio under OSX 10.10, 10.11 (El Capitan) or 10.12 (Sierra) - MTFA/CohortEx GitHub Wiki
Run rJava with RStudio under OSX 10.10, 10.11 (El Capitan) or 10.12 (Sierra)
The R script uses rJava and RJDBC packages to load tables from database. In Mac OS X, to correctly load this packages
requires the following steps borrowed from https://github.com/snowflakedb/dplyr-snowflakedb/wiki/Configuring-R-rJava-RJDBC-on-Mac-OS-X.
Steps have been modified to reflect problems (and it's solutions) reported here.
Overview
This is the list of software downloads needed in order to connect R via RJDBC
to a database:
- Xcode Command Line Tools
- Java 6
- Java 8
- R
- rJava source package
- Database JDBC jar file
- RStudio (optional)
If you have any of this software already installed, you can skip that section.
Open Terminal.app
We'll change to the Downloads directory as we'll have to download some packages from the internet.
cd $HOME/Downloads
Just to make debugging easier if something does not go as planned, log all output.
script osx-rjava-install.log
Xcode Command Line Tools
We need Xcode Command Line Tools, by running this command we will invoke the downloader if not installed, otherwise it will report it is already installed.
xcode-select --install
Java 6
Install the legacy Java 6 runtime which is needed despite us not going to use Java 6.
curl -#ROL https://support.apple.com/downloads/DL1572/en_US/javaforosx.dmg
open javaforosx.dmg
Now walk through the install to complete.
Java 8
Install Java 8. Note: newer versions may be available.
curl -#ROL -b "oraclelicense=a" http://download.oracle.com/otn-pub/java/jdk/8u111-b14/jdk-8u111-macosx-x64.dmg
open jdk-8u111-macosx-x64.dmg
Now walk through the install to complete.
We'll run these two commands to output Java installations and what the current command defaults to. They are informative only.
/usr/libexec/java_home -V
java -version
You should see something like this:
bash-3.2$ /usr/libexec/java_home -V
Matching Java Virtual Machines (5):
1.8.0_111, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home
1.8.0_25, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home
1.6.0_65-b14-468, x86_64: "Java SE 6" /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
1.6.0_65-b14-468, i386: "Java SE 6" /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home
bash-3.2$ java -version
java version "1.8.0_111"
Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode) ```
R for OS X
Download and install the R package for OS X.
curl -#ROL https://cran.rstudio.com/bin/macosx/R-3.3.2.pkg
open R-3.3.2.pkg
Now walk through the install to complete.
Now we need to tell R to use our Java 8 as it's JAVA_HOME
sudo R CMD javareconf
You should see output similar to
Password:
Java interpreter : /Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/bin/java
Java version : 1.8.0_111
Java home path : /Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/jre
Java compiler : /Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/bin/javac
Java headers gen.: /Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/bin/javah
Java archive tool: /Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/bin/jar
Non-system Java on macOS
trying to compile and link a JNI program
detected JNI cpp flags : -I$(JAVA_HOME)/../include -I$(JAVA_HOME)/../include/darwin
detected JNI linker flags : -L$(JAVA_HOME)/lib/server -ljvm
clang -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/jre/../include -I/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/jre/../include/darwin -I/usr/local/include -I/usr/local/include/freetype2 -I/opt/X11/include -fPIC -Wall -mtune=core2 -g -O2 -c conftest.c -o conftest.o
clang -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/usr/local/lib -o conftest.so conftest.o -L/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/jre/lib/server -ljvm -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
JAVA_HOME : /Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/jre
Java library path: $(JAVA_HOME)/lib/server
JNI cpp flags : -I$(JAVA_HOME)/../include -I$(JAVA_HOME)/../include/darwin
JNI linker flags : -L$(JAVA_HOME)/lib/server -ljvm
Updating Java configuration in /Library/Frameworks/R.framework/Resources
Done.
Now install rJava
from source and compile it against the Java 8 JDK.
The 'unset JAVA_HOME' takes care of:
'checking JNI data types... configure: error: One or more JNI types differ from the corresponding native type.'
error.
unset JAVA_HOME
R --quiet -e 'install.packages("rJava", type="source", repos="http://cran.us.r-project.org")'
Now let's check that rJava can be loaded correctly and returns the correct JDK version.
This command should return the string 1.8.0_111-b14
(or whatever Java 8 version you installed).
R --quiet -e 'library("rJava"); .jinit(); .jcall("java/lang/System", "S", "getProperty", "java.runtime.version")'
Finish
Exit from the script logging.
exit
Using RStudio.app or R.app
A common error when trying to load rJava in RStudio is:
Error : .onLoad failed in loadNamespace() for 'rJava', details:
call: dyn.load(file, DLLpath = DLLpath, ...)
error: unable to load shared object '/Library/Frameworks/R.framework/Versions/3.3/Resources/library/rJava/libs/rJava.so':
There are three ways to get around this problem and use rJava
with RStudio or R console (R.app) :
1 - Launching from the command line
You can launch it from the command line in Terminal.app like such:
LD_LIBRARY_PATH=`/usr/libexec/java_home`/jre/lib/server open -a rstudio
LD_LIBRARY_PATH=`/usr/libexec/java_home`/jre/lib/server open -a R
This is a known limitation with RStudio on OS X. See this link for more details: [https://support.rstudio.com/hc/communities/public/questions/200650933-rJava-fails-to-load-in-RStudio-Desktop-OS-X] (https://support.rstudio.com/hc/communities/public/questions/200650933-rJava-fails-to-load-in-RStudio-Desktop-OS-X)
2 - Make a symlink to libjvm.dylib
This is an alternate solution to loading RStudio, but must be updated any time you upgrade Java
+Based on this Stack Overflow thread, one can use the following to work around the libjvm.dylib
loading issues.
sudo ln -s $(/usr/libexec/java_home)/jre/lib/server/libjvm.dylib /usr/local/lib
3 - Load the library in the script (which is the used approach in the CohortEx R script)
Just a bit of code in your R script:
if (Sys.info()['sysname'] == 'Darwin') {
libjvm <- paste0(system2('/usr/libexec/java_home',stdout = TRUE)[1],'/jre/lib/server/libjvm.dylib')
message (paste0('Load libjvm.dylib from: ',libjvm))
dyn.load(libjvm)
}
library(rJava)