Run rJava with RStudio using OSX 10.10
February 20, 2015
Update 2017-02-12: Newer fix available
The workaround described in this post seized working for newer version of rJava
and RStudio
. I wrote a new blog post using Toontje’s fix here: Load rJava in RStudio using macOS 10.12.
The post below refers to older version of rJava
, RStudio
and macOS
.
The Problem
I’ve had some trouble getting rJava to run with RStudio. My current solution is a small workaround based on this post on the RStudio support pages.
The first thing I did was installing and running rJava
through RStudio
and attempt to load it.
install.packages('rJava')
# a bunch of output follows
# ...
# Now we want to load rJava, which yields an error
library(rJava)
Error : .onLoad failed in loadNamespace() for 'rJava', details:
call: dyn.load(file, DLLpath = DLLpath, ...)
error: unable to load shared object '/usr/local/Cellar/r/3.1.2_1/R.framework/Versions/3.1/Resources/library/rJava/libs/rJava.so':
dlopen(/usr/local/Cellar/r/3.1.2_1/R.framework/Versions/3.1/Resources/library/rJava/libs/rJava.so, 6): Library not loaded: @rpath/libjvm.dylib
Referenced from: /usr/local/Cellar/r/3.1.2_1/R.framework/Versions/3.1/Resources/library/rJava/libs/rJava.so
Reason: image not found
Error: package or namespace load failed for ‘rJava’
When I’ve ran R
through the Terminal and ran up library(rJava)
, everything worked just fine. Somehow, RStudio
is not able to properly load the java path. A small workaround helps here.
The Workaround
We will launch RStudio
through the terminal with a custom call, which gives it the proper Java
path!
Check if everything works as expected
At first we check a couple of things.
# `which java` should yield something like this: `/usr/bin/java`
$ which java
/usr/bin/java
# `/usr/libexec/java_home` should yield the current jdk home folder
$ /usr/libexec/java_home
/Library/Java/JavaVirtualMachines/jdk1.8.0_31.jdk/Contents/Home
By the way, you can also use /usr/libexec/java_home
to switch your java version if you wish. You can also download the newest jre
from the Oracle here.
If the output is similar to the one above you should be fine!
Run RStudio
through the Terminal using a custom command
Now you open RStudio
through the terminal using the following command, which sets the LD_LIBRARY_PATH
by hard. Make sure you quit all running instances of RStudio
before you do that!
$ LD_LIBRARY_PATH=$(/usr/libexec/java_home)/jre/lib/server: open -a RStudio
RStudio
launches. Loading library(rJava)
should work just fine! If you want to use rJava
with RStudio
, you have to run it using this command (until they hopefully fix it!)
Make an Alias for the new Command with your Shell
You can register an alias with your shell. For bash
you open the bash profile under ~/.bash_profile
and add the following alias:
alias rstudio='LD_LIBRARY_PATH=$(/usr/libexec/java_home)/jre/lib/server: open -a RStudio'
Thats it
I hope this gets fixed soon. Until then, hopefully my workaround works for you guys! See you :).
Update 2015-12-09
In my current setting of OSX 10.11.2 (15C50), R version 3.2.2 (2015-08-14) and rJava 0.9-8, this solution does not work. Unfortunately, I have no idea how to fix it. Here is my current situation.
Installing the rJava from CRAN fails
Since El Capitan seems to have problems with passing evironment variables, the current CRAN version of rJava will crash during the installation process. It yields the following error:
install.packages("rJava")
# Output until error skipped
configure: error: One or more JNI types differ from the corresponding native type. You may need to use non-standard compiler flags or a different compiler in order to fix this.
ERROR: configuration failed for package ‘rJava’
* removing ‘/usr/local/lib/R/3.2/site-library/rJava’
This can, however, be easily fixed by installing rJava from RForge.net as stated here. Running install.packages('<PATH TO DOWNLOAD>/rJava_0.9-8.tar.gz', repos = NULL, type="source")
installed the current rJava version properly.
Loading rJava in RStudio fails
I’ve set up my homebrew R version properly using R CMD javareconf JAVA_CPPFLAGS="-I/System/Library/Frameworks/JavaVM.framework/Headers -I/Library/Java/JavaVirtualMachines/jdk1.8.0_31.jdk/"
as statet in the caveats of the install. Also, /usr/libexec/java_home
yields the proper path.
Loading rJava in R through the shell works fine. Loading RStudio with LD_LIBRARY_PATH=$(/usr/libexec/java_home)/jre/lib/server: open -a RStudio
and loading rJava does not. It yields the same error as before:
library(rJava)
Error : .onLoad failed in loadNamespace() for 'rJava', details:
call: dyn.load(file, DLLpath = DLLpath, ...)
error: unable to load shared object '/usr/local/lib/R/3.2/site-library/rJava/libs/rJava.so':
dlopen(/usr/local/lib/R/3.2/site-library/rJava/libs/rJava.so, 6): Library not loaded: @rpath/libjvm.dylib
Referenced from: /usr/local/lib/R/3.2/site-library/rJava/libs/rJava.so
Reason: image not found
Error: package or namespace load failed for ‘rJava’
The Solution?
If you have any ideas on how to solve this, please let me know.