Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to configure rJava correctly on macOS
#!/usr/bin/env bash
# Script to configure rJava correctly on macOS.
# Check R is available.
RP=$(which R)
if [ $? -ne 0 ]; then
>&2 echo "Unable to find R executable; please ensure it is installed correctly."
exit 1
fi
# Check whether R is installed via Homebrew.
if $(which brew 2&>/dev/null) && $(brew unlink --dry-run r | grep "$RP" 2&>/dev/null) ; then
RPATH="$(brew --prefix r)/bin"
fi
# If not a Homebrew installation, check for script running under admin account.
if [ -z $RPATH ]; then
if groups $USER | grep -q -w -v admin; then
echo "Please run this script using an administrator account."
exit 2
fi
fi
# Check location of Java installation.
JAVA_HOME=${JAVA_HOME:-$(/usr/libexec/java_home)}
if [ $? -ne 0 ]; then
>&2 echo "Failed to locate JAVA_HOME; make sure Java is installed"
exit 3
fi
echo "Using JAVA_HOME: ${JAVA_HOME}"
# Notify user, and request confirmation.
echo "Ensure you've reinstalled R since updating your Java installation"
read -p "Continue...[Y/n]? " yn
case $yn in
[Nn]* ) exit;;
* ) ;;
esac
# Resolve location of R installation (works on both package-installed & Homebrew).
RPATH=$(dirname $(dirname $(perl -MCwd -e 'print Cwd::abs_path shift' "$(which R)")))
# Check for existence of javareconf script.
JRFILE=$(find "$RPATH" -type f -name 'javareconf' -print | head -n 1)
if [ ! -e "$JRFILE" ]; then
>&2 echo "Failed to find Java configuration script for R (\"javareconf\")"
RPATH=$(brew --prefix r 2&>/dev/null)
if [ $? -eq 0 ]; then
brew info r | grep '\--with-java' >/dev/null
if [ $? -ne 0 ]; then
>&2 echo -e "Please reinstall Homebrew package 'r' with Java support:\n\tbrew uninstall r && brew install r --with-java"
else
>&2 echo -e "Found Homebrew 'r' installation, but no \"javareconf\"; maybe try reinstalling?\n\tbrew uninstall r && brew install r --with-java"
fi
else
>&2 echo -e "Please check R installation, or reinstall."
fi
exit 4
fi
# Run javareconf script with custom variables.
LIBJVM=$(find "${JAVA_HOME}" -name 'libjvm.dylib')
R CMD javareconf JAVA_LIBS="$LIBJVM" JAVA_LD_LIBRARY_PATH="$LIBJVM" JAVA_CPPFLAGS="'-I${JAVA_HOME}/include -I${JAVA_HOME}/include/darwin -I$(dirname "$LIBJVM")'"
# Link JVM library to allow R to find it.
ln -fs "$LIBJVM" "$RPATH/lib"
@gwinstanley

This comment has been minimized.

Copy link
Owner Author

gwinstanley commented Nov 16, 2016

For more information on this script, see the documentation page for the process. If you're also interested in the background of how it came to be, see this support thread for the rJava project.

@olegursu

This comment has been minimized.

Copy link

olegursu commented Jan 25, 2017

Thank you for sharing, works perfect!

@KKulma

This comment has been minimized.

Copy link

KKulma commented Nov 9, 2017

the link to documentation page is dead, could you post an updated link? thanks!

@abautistah

This comment has been minimized.

Copy link

abautistah commented Nov 27, 2017

this is golden! Thank you, I've been trying to solve this the whole day! cheers!

@OceanGirl07

This comment has been minimized.

Copy link

OceanGirl07 commented Dec 20, 2017

Hey, gwinstanley, thanks for doing this! I was wondering if you could tell me where to run this (I am pretty new to Studio). I opened the terminal in my Mac and ran this, and got the errors in the screenshot.

I am trying to use RWeka in Studio, and get this error. I am running OS X El Capitan and have Java 10.11.6. I am running Studio 1.0.143. I installed the package "rJava" with no problems, but when I load it get a similar error (also pasted below).

Error: package or namespace load failed for ‘RWeka’ in get(Info[i, 1], envir = env):
lazy-load database '/Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava/R/rJava.rdb' is corrupt
In addition: Warning messages:
1: package ‘RWeka’ was built under R version 3.4.2
2: In get(Info[i, 1], envir = env) : internal error -3 in R_decompress1
screen shot 2017-12-20 at 11 00 39 am

Error: package or namespace load failed for ‘rJava’ in get(Info[i, 1], envir = env):
lazy-load database '/Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava/R/rJava.rdb' is corrupt
In addition: Warning messages:
1: package ‘rJava’ was built under R version 3.4.2
2: In get(Info[i, 1], envir = env) : internal error -3 in R_decompress1

@gwinstanley

This comment has been minimized.

Copy link
Owner Author

gwinstanley commented Jan 17, 2018

@OceanGirl07, apologies for the delay; busy offline for a while.
This script is designed to be run in a macOS Terminal session, in other words you need to launch the Terminal application found in the /Applications/Utilities folder. Be aware though, that since you're using R and rJava there's already a strong likelihood you'll be needing some basic knowledge of using terminal sessions for other work/issues, so I strongly recommend you find some online resources to get you started. If you need these tools for the longer term, then knowledge of terminal use will be invaluable for data cleaning/processing/parsing/etc. anyway, and has the potential to save you many hours of work, especially when scripted. Search for resources on { macOS, bash, terminal, tutorial } as a start point. Hope that helps.

@fgravey

This comment has been minimized.

Copy link

fgravey commented Sep 12, 2018

Wonderful !!! Thank you so so much !!! works perfect, many thanks for shearing this wonderful script !!

@xslates

This comment has been minimized.

Copy link

xslates commented Sep 26, 2018

A GOD amongst men! Thank you!

@JohannesFriedrich

This comment has been minimized.

Copy link

JohannesFriedrich commented Jan 6, 2019

Thank you very much!!! This script is fantastic!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.