13  R

Caveat

Notes on all of the things I’ve had to look up and learn about in R could fill volumes. The info stashed here pertains more to meta-R issues I’ve encountered (and am likely to encounter again).

For a much more comprehensive/useful resource on such matters, I highly recommend you check out What They Forgot to Teach You About R AKA rstats.wtf (Bryan et al. 2023).

13.1 Handling rJava installation failures on an M1 Mac

The rJava (Urbanek 2023) package is great, but configuring Java and R to work together nicely can be a struggle. What follows is how I dealt with a very specific problem where I was getting the following error when trying to build rJava on my M1 Mac:

R Console
# configure: error: Java interpreter '/usr/bin/java' does not work

Given I had homebrew installed (see Chapter 5 for details on my homebrew configuration), I was able to tell that Java was already installed on my machine — trying to install it returned a warning that it had already been installed:

Terminal
brew install java
# Warning: openjdk 21.0.1 is already installed and up-to-date.

This brought me to a helpful StackOverflow thread, How to brew install Java?, where several users, including Mark Simon in his response, pointed out that you need to symlink the homebrew-installed openjdk1 so that the system’s Java wrappers could find it.

In my case, I had to run:

Terminal
sudo ln -sfn /opt/homebrew/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk

There are several variations on this command in that thread, but you can find out which one you need for your setup by running brew info openjdk.

I then tried again to install/build rJava with pak, and got the following helpful info just before the ERROR message:

R Console
# Make sure you have Java Development Kit installed and correctly registered in R.
# If in doubt, re-run "R CMD javareconf" as root.

Indeed, running R CMD javareconf from the terminal did the trick.

Terminal
R CMD javareconf

And now I have the latest rJava installed and running!

Tip

I’ve subsequently run into other errors in installing/compiling rJava where running R CMD javareconf seemed to solve my problems! So, if you’ve got the Java Development Kit installed, it’s worth giving it a whirl.

13.2 How to specify a libxml2 library for renv

The renv package is great. It helps you create reproducible environments for your R projects that with libraries that are isolated and portable. I really can’t recommend it enough.

You can also use renv with Quarto2! This is all good news. However, I seem to run into a problem when installing R packages (e.g. XML, and xml2) that have libxml2 as a system requirement. In my case, R finds a version of libxml2 on my machine (seemingly one related to miniconda), but it’s mismatched with the version in header files(?)…summary point: the computer gets angry, and the installation fails. 3

I don’t have this problem in my global R environment, and I’m not totally sure what the interaction is. But, I know the solution lies in using a consistent version of libxml2 (one >= 2.6.3, as is required for the XML package).

Since MacOS provides libxml2, the homebrew libxml2 formula is “keg-only” (i.e. not symlinked into /opt/homebrew). You can see this info and more about your homebrew-installed libxml2 by running:

Terminal
brew info libxml2

The output also includes a helpful hint about how to put homebrew’s libxml2 in your PATH. This, it turns out, solves my problem. The .Renviron file contains a list of environmental variables to set during R Startup. For example, in my global ~.Renviron, I tell R/RStudio about homebrew:

~.Renviron
PATH="/opt/homebrew/bin:${PATH}"

You can also have project-level .Renviron files, which is what I use in my renv project to tell R to use homebrew’s libxml2:

myproject/.Renviron
PATH="/opt/homebrew/opt/libxml2/bin:/opt/homebrew/bin:${PATH}"

Tada! Problem solved. I can renv::install() my libxml2-dependent packages and be on my merry way.


  1. openjdk is the development kit for the Java programming language.↩︎

  2. Other options for virtual enviornments with Quarto include venv and conda.↩︎

  3. This happens regardless of whether or not I have pak enabled in my renv configuration.↩︎