5
votes

I've found a couple bookdown documents on github (namely Hadley's R for Data Science and Efficient R) that I'd like to build from source as ebooks. Bookdown, which the documents are written in, has a couple functions, namely epub_book() and kindlegen() to build a ebook (rather than github book site), which I'd like to use so I can read the books on my Kindle.

I've looked at the bookdown documentation for those functions, but I don't understand how/where to use those functions to build the ebook files and/or if there are output options already written in the code that I need to edit.

2
Did you successfully build R for Data Science into an epub? If so could I ask how? I'm having trouble compiling strings.Rmd, with an error recommending I use always_allow_html: trueScottmeup
It's been a while since I tried this, but I don't remember having much success.crazybilly

2 Answers

5
votes

They are the structure. You should do something like this

render_book("file.Rmd", epub_book())

to build the ebook. Some variables can be passed to epub_book().

You could take a look to https://bookdown.org/yihui/bookdown/ . It is a book about bookdown written in bookdown.

2
votes

A way you can use the kindlegen() function manually is the following:

  1. Install Kindlegen (which is a free command line tool from Amazon to convert .epub into .mobi).
  2. Add the path of Kindlegen to the PATH environment variable.
  3. Run R studio, build the .epub version (Build panel (upper right part by default) > Build book > bookdown::epub_book (depending on your _output.yml configuration)).
  4. After you already have the epub version, in the Console panel (lower left part by default), run bookdown::kindlegen().

Kindlegen will figure out where to find the .epub version, and convert it to .mobi.


To add this to the build process, your _build.sh could look something like this (note the last line):

#!/bin/sh

set -ev

Rscript -e "bookdown::render_book('index.Rmd', 'bookdown::html_book')"
Rscript -e "bookdown::render_book('index.Rmd', 'bookdown::pdf_book')"
Rscript -e "bookdown::render_book('index.Rmd', 'bookdown::epub_book')"
Rscript -e "bookdown::kindlegen()"

And your _output.yml needs to define config for html_book, pdf_book, epub_book, like this:

bookdown::html_book:
  toc: yes
  css: style.css
  split_by: chapter
bookdown::pdf_book:
  includes:
    in_header: preamble.tex
  latex_engine: xelatex
  citation_package: natbib
  keep_tex: yes
  template: null
bookdown::epub_book: default

After this, Build book > All formats should produce all HTML, PDF, EPUB and MOBI formats.