Building R packages: missing path to pdflatex


Recently whiling trying to build an R package for generalized estimating equation model selection (QICpack on github), I was getting an error related to latex creating the PDF package manuals. It seems like this is a relatively common problem on some versions of Mac OS X, but I did not find it easy to find an answer so I thought I’d describe my ultimate solution.

The specific error I was getting was:

error in texi2dvi(file = file, pdf = true, clean = clean, quiet = quiet, : 
pdflatex is not available

I was getting this error regardless of whether building the package using R CMD packagename in the Terminal or using build in R or Rstudio via the devtools package.

I tried reinstalling MacTex and restarting my computer to no avail. It turned out that it was a problem with not having a path to the Tex installation. You can check using the following code in the Terminal or in R:

Sys.which("pdflatex")
Sys.getenv("PATH")

In my case, I got something like:

Sys.which(“pdflatex”)

 ""

Sys.getenv(“PATH”)

"/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin"

I don’t know much about setting paths, profiles, and environments, but this indicated that there is no path to latex and so the build function can’t find pdflatex needed to convert the .Rd files (LaTeX code of manuals in R packages) to PDF files for the R package. I was able to fix the problem by adding a path to the latex binaries using:

Sys.setenv(PATH=paste(Sys.getenv("PATH"),"/usr/texbin",sep=":"))

Now when I run Sys.getenv("PATH") I get

"/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/texbin"

Now R is able to find the LaTeX binaries and when I run Sys.which("pdflatex") I get

              pdflatex 
"/usr/texbin/pdflatex"

That should fix the problem and now the build function can turn the .Rd files into PDF documents of the R functions.

About these ads

About Daniel Hocking

I am a post-doctoral researcher at the UMass-Amherst. I am interested in the use of statistical models in ecology and population biology.

Posted on December 15, 2012, in R and tagged , , , , , . Bookmark the permalink. 2 Comments.

  1. That was a superb post. Pandoc and I were previously acquainted, but now we’re best friends!

  1. Pingback: Knitting beautiful documents in RStudio | Daniel J. Hocking

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 51 other followers

%d bloggers like this: