LittleInferenceBook icon indicating copy to clipboard operation
LittleInferenceBook copied to clipboard

Knit Rmd to LeanPub and pdf

Open odeleongt opened this issue 10 years ago • 4 comments

LeanPub seems like a nice platform for publishing, but it seems very limiting for this book since the format they use is not directly compatible with the R workflow heavily used in the book. This pull request addresses this issue by providing facilities to prepare the manuscript in Rmd format and knitting to produce LeanPub compatible markdown files and figures, thus rendering the book reproducibly.

If you are interested in this, I would advice that you reject this pull request and create a local branch to track my knitr-rmd-to-LeanPub branch using:

git remote add odeleongt https://github.com/odeleongt/LittleInferenceBook.git
git fetch odeleongt
git checkout --track odeleongt/knitr-rmd-to-LeanPub

and then checkout the branch to review the changes and test the new code. If you find this convenient, you can merge the new branch to your master since at this point they seem compatible:

git checkout master
git merge odeleongt/knitr-rmd-to-LeanPub

I'll provide some additional details and list a few drawbacks in the next comment in this pull request.

odeleongt avatar Mar 05 '15 02:03 odeleongt

The knitr-rmd-to-LeanPub branch contains your LeanPub md chapters fixed to work as Rmarkdown documents. The changes include:

  • md -> Rmd due to equation markup ([$$]...[/$$] to $...$ or $$...$$ according to context)
  • Remove equation markers from explicit equation environments (\begin{eqnarray*} fails inside math mode)
  • LeanPub code delimiters to Rmarkdown chunks
  • Leave only code or comments in code chunks
  • Configure code chunks to match output as shown in the LeanPub book (as closely as posible, see odeleongt/LittleInferenceBook#1)
  • Produce LeanPub compatible markdown as a side effect of knitting the Rmd files. This is achieved by using hooks which revert with regular expressions the formatting changes mentioned above.

The biggest drawback of this implementation is that it only considers LeanPub specific formatting which is currently in use in your book. It shouldn't be difficult to implement more LeanPub features, but it surely will require work.

The image sizes saved by knitr possibly do not match the original images in the book, but I had no way to check without submitting the book to LeanPub so I don't know.

Figures involving randomization are reproducible because I added a set.seed just before book knitting, but if a user copies the code for the image she might get a different result (see odeleongt/LittleInferenceBook#2).

Some figures can not be reproduced from the code shown in the book, since they require evaluating code not shown in the context (see odeleongt/LittleInferenceBook#3)

odeleongt avatar Mar 06 '15 15:03 odeleongt

Thanks; this is very cool. I'll have to look into this more carefully.

bcaffo avatar Mar 25 '15 01:03 bcaffo

Yep, good idea. I fixed some conflicts and introduced the new changes into the Rmd sources, so it is still compatible with your master branch.

Also, in case it is needed (as suggested in Hadley's ggplot2-book):

I assign the copyright of this contribution to Brian Caffo.

odeleongt avatar Mar 25 '15 03:03 odeleongt

Also, to produce the book from the odeleongt/knitr-rmd-to-LeanPub branch, just source(knit_book.R) from the repository's root directory after you have reviewed the changes.

It prepares an Rmd file to compile the book from the Rmd sources, and outputs the LeanPub compatible md files as a side effect. In any case, if it breaks some day at least you will have the most recent md files.

odeleongt avatar Mar 25 '15 03:03 odeleongt