Building a Book

Building a Book
Author

An Phan

Published

February 9, 2023

What we want: build a book

Build the html version of the 2nd edition for Hadley Wickham’s book Advanced R Programming from a github repository.

Deliverable (1): what is the last word in chapter 15? - You don’t have to answer this question, if things don’t work.

After struggling with so many errors, I was able to build a book under two formats: html (index.html) and pdf (_main.pdf). Surprisingly, chapter 15 ends differently for these two formats.

  • For main.pdf: the last sentence is exercise number 2 of section 15.6.3 “Define a length method for the Person class.” (attached image _main.pdf.png)
  • For index.html: the last sentence looks like a footnote (or description, or citation) numbered 81 “The S4 ANY pseudo-class plays the same role as the S3 default pseudo-class.” (attached image index.html.pdf) I think this was not added for compiling in the pdf format.

Things that did not work:

Deliverable (2): Write a paragraph on the biggest problem you encountered when you were building the book, (and how you managed to solve it.).

I decided to describe the most frustrating problems I faced:

  • A bunch of packages that were not installed kept interrupting the render process (e.g., htmltools, bookdown, cli, even Rcpp). I tried to install each and every package when required. Eventually, I found a smarter solution to install all required packages automatically.The script filters for library files in the project and auto-installs them almost perfectly.
  • Quitting from lines 4-5 (Introduction.Rmd) Error in get(genname, envir = envir) : object ‘wrap’ not found. For this, I tried looking at the code chunk, then consulted the Q&A in Canvas, and changed “wrap” to “sew”.
  • Quitting from lines 106-107 (Control-flow.Rmd) Error in if (c(TRUE, FALSE)) 1 : the condition has length > 1. We receive an error message because we passed a vector to the if() statement, instead of a warning. At first, I commented that line out (in Control-flow.Rmd) for a quick solution. Then I switched to a more appropriate approach as suggested on Canvas, that is setting error=TRUE for that specific chunk. This option let me continue rendering the book (do not stop on error) as well as show the error on purpose in the book.
  • Some packages are “not available for this version of R”. I was using R 4.2.1 on a department PC at this point, which I cannot update R by myself (only certain versions of software are available on Software Center) so I had to find an alternative solution. I installed package emo from source by cloning Hadley’s repo into the local repo:
$ git clone --recurse-submodules https://github.com/hadley/emo.git
# install.packages("emo2",repos=NULL,type="source")
library(emo)

This is another way to install packages that are not on CRAN.

  • Quitting from lines 183-185 (Quotation.Rmd) Error in file(con, “w”) : cannot open the connection. This occurred multiple times (I found other classmates also faced the same problem), I decided to rebuild without changing anything and it worked.
  • The last error: Error: LaTeX failed to compile main.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. which took me a lot of time. After installing, re-installing, uninstalling desperately, I went back to read the error details more closely, and figured that some fonts are missing, so the tinytex package is not the main problem. I was working on Ubuntu then, so I decided to install two fonts that were missing (Inconsolata, Andale Mono, found in preamble.tex) from the terminal and refreshed the cache to update new fonts:
$ sudo apt-get install fonts-inconsolata -y
$ sudo apt-get install ttf-mscorefonts-installer
$ sudo fc-cache -fv

One last TeX error was solved by adding \usepackage{hyperref} in preamble.tex following Yihui’s solution.

And there we have it, the book has been successfully built entirely from R, with the help of LaTeX and some Cpp code. Now the errors feel less frustrating to deal with, I guess I have encountered around 10% of all possible errors 😄 🙌 🎉 🎉!