bookdown: reveal solution to exercise

bookdown

#1

Hi. I am wondering if anyone knows if it is possible in bookdown to have an answer to an exercise reveal itself upon clicking a button or an icon. A good example (the inspiration for this question) can be found in lessons in The Carpentries workshops such as this one, which has four instances. And, the ggplot lesson has a few examples of how this looks with R code and output.


#2

That ggplot2 Carpentries example actually starts life as an Rmd file. If you take a look at the source, you'll see that they seem to have added some CSS classes to the blockquote elements by hand, and then there's a bit of Javascript that does the folding magic.

I don't know if there would be any further challenges to using this method in the context of bookdown (so if you try it, post back how it goes! :grin:).


#3

This kind of feature is included in learnr :package: but I don't know if the solution feature could be extracted to work on bookdown. It seems possible looking at how to create custom format.

Similarly, you have also unilur :package: features that could be useful here. I think its custom blocks (including a solution one) can be used in bookdown and it is pretty straightforward.

Also, there was a book which participate to bookdown contest that include lots of feature regarding teaching. See the community post. It is pretty advanced but it showcases how it is possible to do so with several features (small one like a hide button, and more advanced with inclusion of datacamp.js feature to do exercice in a R console inside the book)


#4

I'm a complete bookdown newbie, but I'm interested in having collapsable and customisable boxes like those in the unilur package in my bookdown document but I'm having trouble combining the two though.

This works in a normal Rmarkdown document:

---
title: "Untitled"
output: unilur::tutorial_html_solution
---

```{r}
library(unilur)
```

```{block, box.title = "You can add a title", box.body = list(fill = "lightblue"), box.header = list(fill = "red", colour = "white")}
A custom box
```

But when I try to run the same code in a bookdown document it doesn't work as expected.

Here is the code I'm trying in a fresh bookdown template that you get when starting a bookdown project in Rstudio. My index.Rmd looks like this (nothing else is changed):

--- 
title: "A Minimal Book Example"
output: unilur::tutorial_html_solution
site: bookdown::bookdown_site
---


# Test heading

```{r}
library(unilur)
```

```{block, box.title = "You can add a title", box.body = list(fill = "lightblue"), box.header = list(fill = "red", colour = "white")}
A custom box
```

I can render the book using:
bookdown::render_book("index.Rmd", "bookdown::gitbook")
or
bookdown::render_book("index.Rmd", "unilur::tutorial_html_solution")

but they both don't work in different ways. In the first, the block doesn't get formatted (but the text shows) and using the second, the block gets formatted as expected, but the book becomes one long concatenated document (as far as I can tell).


#5

I'll try to give it a look. It is possible it is not compatible though... I never tried to include this in bookdown. I think it is possible to reuse the idea of custom block that is a feature of bookdown to do the same think like in unilur. You should look in this direction rather than using the whole document format of unilur


#6

That makes a whole lot of sense! Thanks @jcblum. I'm going to take a bit of time trying to integrate the CSS and Javascript from the Carpentries as well as the other suggestions from @cderv (thank you!) below. @mrblobby, I will post here when I have something figured out.

I have a feeling that the easiest solution for me will be to clone the Econometrics repository and remove all of the stuff I don't need since they seem to have already figured out how to integrate Javascript into Bookdown (I'm not too proficient in Javascript or programming more generally; just R). I'm pretty sure that it will take me some time to figure out how to do the same with the learnr and unilur packages as well as The Carpentries styles.