Free Book: Data Science at the Command Line

The entire book has been converted to R Markdown and can now be read online for free.
Jeroen Janssens

January 23, 2018 • 3 min read

I’m thrilled to announce that my book “Data Science at the Command Line” can now be read online for free at datascienceatthecommandline.com! I hope that this way, many more people will be able to learn about this exiting piece of technology called the command line. After having seen the website of “R for Data Science” by Hadley Wickham and Garrett Grolemund, I also wanted to make use of bookdown, an R package that takes R Markdown and produces such as PDF, EPUB, and HTML. On bookdown’s website there are many more examples of books.

Converting AsciiDoc to R Markdown

I wrote my book originally in a format called AsciiDoc, so I first had to convert everything to R Markdown. To be honest, this conversion was quite involved. To give you an idea, here’s one of several quick-and-dirty Bash scripts I hacked together (you’ll have to excuse the mess):

#!/usr/bin/env bash
asciidoc -b docbook -o - - |                              # convert asciidoc to docbook
iconv -t utf8 |                                           # ensure it's encoded in UTF-8
pandoc -f docbook -t markdown --wrap=none --atx-headers | # convert to markdown
iconv -f utf-8 |                                           
sed -re 's/``` \{.console\}/```\{bash, eval=FALSE\}/' |   # change type of code chuck
sed -re 's/\\\[cite:([^\]+)\\\]/\[@\1\]/g' |              # fix citations
sed -re 's/\\\[citenp:([^\]+)\\\]/@\1/g' |                # fix citations
sed -re '/^#+/s/\{#(.*)\}$//' |                           # remove heading ids
sed -re '/\*\*Caution\*\*/,/^$/{s/^> \*\*.*$/```\{block2, type="rmdcaution"\}/;s/^$/```\n/;s/^> ?//}' | # notes, tips, cautions
sed -re '/\*\*Tip\*\*/,/^$/{s/^> \*\*.*$/```\{block2, type="rmdtip"\}/;s/^$/```\n/;s/^> ?//}' |
sed -re '/\*\*Note\*\*/,/^$/{s/^> \*\*.*$/```\{block2, type="rmdnote"\}/;s/^$/```\n/;s/^> ?//}' |
sed -re '/\*\*Important\*\*/,/^$/{s/^> \*\*.*$/```\{block2, type="rmdimportant"\}/;s/^$/```\n/;s/^> ?//}' |
sed -re '/^!\[/s/^!\[(.*)\]\((.*)\)/```\{r, echo=FALSE, fig.cap="\1", fig.align="center"\}\nknitr::include_graphics\("\2"\)\n```/' |  # figures
sed -re 's/\[figure\\\_title\]\(\#([^\)]*)\)/Figure\n\\@ref\(fig:\1\)\n/g' |   # figure references
sed -re 's/\[example\\\_title\]\(\#([^\)]*)\)/Example\n\\@ref\(exm:\1\)\n/g' | # example references
sed -re '/^\\@ref/y/_/-/' | # replace underscores with dashes in references
sed -re '/\\\[render:/d' |
sed -re '/^-/{N;N;s/^\-(.*)\n\n\-(.*)$/\-\1\n\-\2/;}' | sed -re 'N;/^-/{N;N;s/^\-(.*)\n\n\-(.*)$/\-\1\n\-\2/;}' | # fix empty lines in lists 
sed -re 's/``` \{\.(.*)\}/```\{example, name=""\}\n```\n```\{\1, eval=FALSE\}/' # create examples

Fortunately, once you do have some proper R Markdown, bookdown turns it into a pretty website. All in all, I’m really happy with the outcome.

Many thanks to O’Reilly for allowing me to put the book online for free.

Related