Brett Klamer

Compile Hadley's Advanced R to a PDF

Hadley Wickham's book, Advanced R, is now published through Chapman and Hall. The source is available on GitHub and a version compiled for the web is at http://adv-r.had.co.nz/. Even though the HTML format is nice, I still like to have a PDF around. Here's how to compile it from the source. (Please consider buying a copy to support his work.)

You will need R, LaTeX, pandoc, and knitr to create the PDF.

Download the Source

Extract to your folder of choice. https://github.com/hadley/adv-r/archive/master.zip

Install R Package Dependencies

First check if you have the latest version of R installed (and install Rtools if on a Windows OS) and update all installed packages.

# Install CRAN packages
install.packages(c("devtools", "rmarkdown", "inline", "pryr", 
                   "microbenchmark", "dplyr", "png", "knitr"))

# Install development packages
## Note that adv-r still uses the old bookdown package Hadley originally
## developed. The new bookdown package is being developed by rstudio/Yihui Xie.
## In addition, adv-r only compiles with an old version of the old bookdown.
## I've forked that old version and you can install it from my github.
## Note that adv-r still uses the lineprof package which has been deprecated
## by the profvis package.
devtools::install_github(c("bklamer/olderbookdown", "hadley/lineprof"))

Install Missing Pandoc Packages

If citeproc is needed…

# For ubuntu users...
sudo apt-get install pandoc-citeproc

Install TeX Dependencies

If you have never used XeLaTeX, there may be a few packages you will need to manually install in MiKTeX or TeX Live.

Create /figures and delete /book/tex/*.tex

Create the folder /figures in the sources extraction location. Hadley has recently included the .tex files in the /book/tex folder, but still has the /figures folder in the .gitignore. To create those figures, simply delete all the .tex files in the /book/tex folder and recompile them again in the next step.

Revert Commit

The latest commit to book/advanced-r.tex broke the frontmatter and table of contents. To fix this, use the following version of advanced-r.tex.

Fix Code Errors

Code currently producing errors:

  1. Change line 159 in file Function-operators.rmd to slow_function <- function() {

Use Hadley's Build Script

  1. Double click the adv-r.Rproj file and open with RStudio.
  2. Open the /book/build-book.r file.
  3. Run the build-book.r file line by line to see how it works and catch any errors. You're most likely to run into issues at line 36: lapply(chapters, render_chapter) or line 72 when the LaTeX compilation starts.
    • If using Windows, edit the functionals.rmd file so all occurrences of mc.cores = X are changed to mc.cores = 1. You can only use more than 1 core on Linux or Mac. You may also need to do this with profiling.rmd.
  4. Debug any other errors as best you can. A few lines of R code in the book are designed to produce errors. If using Linux, you can probably ignore the libpng warnings while compiling the tex files. Setting options(error=recover) may help debugging the compilation of tex files.

My resulting PDF is 2.4MB and 475 pages as of 2015-10-01. It seems to be an exact duplicate of the html version (although there may be some errors hiding in the corners).

Better PDF Output

Now you can modify the LaTeX source to change the book's appearance. For example, modify the document class of book/advanced-r.tex with

\documentclass[letterpaper, 11pt, oneside]{scrbook}

Then re-run book/build-book.r starting at line 61 or wherever the LaTeX compilation begins. The sky is the limit for any other changes you wish to make…

Last updated 2016-03-26 - Feel free to email me with any questions or comments.