Skip to content

Instantly share code, notes, and snippets.

@cszang
Created February 20, 2015 12:16
Show Gist options
  • Save cszang/ae99fccc2584bbf7175a to your computer and use it in GitHub Desktop.
Save cszang/ae99fccc2584bbf7175a to your computer and use it in GitHub Desktop.
Build Advanced R Book as Epub.
## we need the libraries bookdown, knitr, inline, markdown, pryr
system("git clone https://github.com/hadley/adv-r.git")
setwd("adv-r")
library(knitr)
embed_png <- bookdown:::embed_png
rmds <- list.files(pattern = "^.*rmd$")
sapply(rmds, function(x) knit(x))
cat("---\ntitle: Advanced R\nauthor: Hadley Wickham\nlanguage: en-US\n...\n",
file = "front_matter.txt")
## assuming we installed pandoc via cabal
pandoc <- "~/.cabal/bin/pandoc"
files <- c("front_matter.txt",
"Introduction.md",
"Data-structures.md",
"Subsetting.md",
"Vocabulary.md",
"Style.md",
"Functions.md",
"OO-essentials.md",
"Environments.md",
"Exceptions-Debugging.md",
"Functional-programming.md",
"Functions.md",
"Function-operators.md",
"Computing-on-the-language.md",
"Expressions.md",
"dsl.md",
"Performance.md",
"Profiling.md",
"memory.md",
"Rcpp.md",
"C-interface.md")
build_cmd <- paste(pandoc, "-S -o adv-r.epub", paste(files, collapse = " "))
system(build_cmd)
@fernandovmacedo
Copy link

How the line "embed_png <- bookdown:::embed_png" works?
I've tried to run the script but my final epub document doesn't show the diagrams properly. Any ideas?
EDIT: Nevermind, it was a bug on the pandoc version in ubuntu 14.10. I've downloaded the most recent release directly from the pandoc site, reinstalled pandoc, rerun the script and the images were fine. Thank you for posting it. I still don't know how that line works, but now I have the book!

@tetotechy
Copy link

thanks @cszang for this. please note there is a typo @ line 23. it should read "Functionals.md" instead of "Functions.md". as it is, the chapter on Functions is included twice

@mkempenaar
Copy link

In combination with the above comment; read the instructions for creating a PDF version of this book by Brett Klamer who notes that you need an older version of the bookdown library which he hosts at GitHub and can be installed with devtools::install_github(c("bklamer/olderbookdown")), otherwise the embed_png function is not found.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment