Bookdown Python
Bookdown + exams + webex. First and foremost, the main part of the hack is to realize that any exercises in a.Rmd file can be broken into a list using exams::xexams.Let’s use an example from the book, with the first three exercises of chapter 01. Bookdown-Specific Features. Before you can use blogdown-specific features, you need to first open the directory of your bookdown project. The easiest way is to use RStudio's 'New Project'. In the command palette (Ctrl/Cmd+Shift+P), search for serve book; Execute Blogdown: Serve Site; Click the link in the output to view your book.
It’s been three years since I’ve been using package bookdown
for compiling and distributing three different books in Amazon and the web. It helped me greatly in all my book projects and I’m always grateful to Yihui Xie for providing such a useful tool at the right time.
However, bookdown
offers no support for chapter exercises of any sort. While you can write exercises in plain RMarkdown, it is not a good solution for a long term project such as a technical book. When writing the latest edition of Analyzing Financial and Economical Data with R, I aimed for a work cycle where the 100 plus exercises and their solutions were reproducible and easier to maintain.
Meanwhile, package exams
provides a framework to produce exercises in a reproducible setup, making it possible to export the exercises to any given format such as pdf or html, or even e-learning platforms such as Moodle and Blackboard. I use exams
extensively in all my university classes and it works like a charm!
So, while writing afedR, I worked towards finding a way to bring the two technologies closer to each other, which is what I’ll report in this blog post. Here are the main advantages of this setup:
- The content of book exercises, their solution and explanation in a single location (no more fidling with different folders).
- Dynamic output for html, with buttons and solutions available at a single click.
- Exportable exercises for classes (see this blog post). You can export the same exercises to pdf or Moodle, for example.

bookdown
+ exams
+ webex
First and foremost, the main part of the hack is to realize that any exercises in a .Rmd file can be broken into a list using exams::xexams
. Let’s use an example from the book, with the first three exercises of chapter 01:
As an example, in this list you can see the main text of the question 01 in slot l_out$exam1$exercise1$question
:
And the solution at my_l$exam1$exercise1$solution
In my case, I wanted the html version of the book to have all the solutions hidden by a clickable button – just like in webex
– while the pdf and ebook would only have the text of the questions. Here are the functions I used:
Html Exercises
The html output for the selected three exercises is given next. Do notice that the correct solution is not highlighted in this blog post due to the lack of css and javascript. In the final result you’ll see that it works correctly. Also, you’ll need to set results='asis'
in the knitr options of the chunk (the code output pure html).
Q.1
The R language was developed based on what other programming language?
Solution: The solution is S. To reach the same result, you must execute the code below. For that, open a new R script in RStudio (Control+shift+N), copy and paste the code, and execute it whole by pressing Control+Shift+Enter or line by line with shortcut Control+Enter.

Q.2

What are the names of the two authors of R?

Straight from the book: “… The base code of R was developed by two academics, Ross Ihaka and Robert Gentleman, resulting in the programming platform we have today.”.
Q.3
Why is R special when comparing to other programming languages, such as Python, C++, javascript and others?
Solution: The solution is It was designed for analyzing data and producing statistical output. To reach the same result, you must execute the code below. For that, open a new R script in RStudio (Control+shift+N), copy and paste the code, and execute it whole by pressing Control+Shift+Enter or line by line with shortcut Control+Enter.
Bookdown Python Code
Pdf/Ebook Exercises
And for latex (pdf) and epub3 (ebook), the result is:
01 -The R language was developed based on what other programming language?
- C++
- S
- Javascript
- Julia
- Python
02 -What are the names of the two authors of R?
- Guido van Rossum and Bjarne Stroustrup
- John Chambers and Robert Engle
- Roger Federer and Rafael Nadal
- Ross Ihaka and Robert Gentleman
- Linus Torvalds and Richard Stallman
03 -Why is R special when comparing to other programming languages, such as Python, C++, javascript and others?
- Works on any plataform such as Windows, Unix, MacOS
- Easy to use
- Quick code execution
- Makes it easy to write mobile apps
- It was designed for analyzing data and producing statistical output
Bookdown Python Programming
As you can see, it works great. So, at the end of each chapter I simply called function compile_eoc_exercises()
with the knit chunk options results='asis'
and echo=FALSE
. Moreover, object my_engine
is set as my_engine <- knitr:::pandoc_to()
, which will figure out the format within the compilation of the book:
Conclusion
Its is amazing how much we can accomplish by learning and mixing different technologies. In this case, I used R, Latex, html, javascript and css to bundle reproducible and dynamic exercises for my book. You can find examples of the final output in html, pdf and ebook.
Bookdown Python Compiler
If you’re trying it for you own book, make sure to add the correct .js and .css files to the html compilation. In my case, I used my_javascript.js and style_html.css.
