How to make R recognize command line software installed on Mac OS with Homebrew

Imagine you installed an R package that depends on some command line software, for example pdftotext. You’ve successfully installed it using Homebrew, but when you run the R package, you get an error that looks like this:

sh: pdftotext: command not found

So you double-check that pdftotext is installed on your system in the Terminal.

$ which pdftotext

So far so good. Then you double-check that pdftotext is available to R.

> Sys.which("pdftotext")

Uh oh. The path to pdftotext should be inside the second, empty set of quotes there.

What this means is that your shell’s PATH differs from R‘s. So the place that your Terminal looks up what programs are available to it is different from the place that R looks up what programs are available to it.

You can tell what paths are available to the Terminal and which ones are available to R by typing the following in the Terminal:

$ printenv PATH

And the following in your R console:

> Sys.getenv("PATH")

At this point you should see what the differences are, and which ones are missing. Probably what’s missing is the Homebrew directory, /opt/homebrew/bin.

So how do you fix this? We need to tell R on startup to look for programs installed by Homebrew.

If it doesn’t already exist, make an empty text file in your home directory called .Rprofile. Edit this file using your text editor of choice (E.g. R Studio) so that it includes the following:

old_path <- Sys.getenv("PATH")
Sys.setenv(PATH = paste(old_path, "/opt/homebrew/bin", sep = ":"))

When you restart R, your Homebrew-installed R package should now function!

Published by

The Grey Literature

This is the personal blog of Benjamin Gregory Carlisle PhD. Queer; Academic; Queer academic. "I'm the research fairy, here to make your academic problems disappear!"

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.