hopr icon indicating copy to clipboard operation
hopr copied to clipboard

feature: make all solution chunks foldable and folded by default

Open dlaehnemann opened this issue 1 year ago • 1 comments

I used this book to give an intro into R course, and it was very useful to teach both people without any prior programming or R experience, and those that have already worked a bit in R. It enabled self-paced studying, with help among participants and by the instructor. So thanks a lot for this resource!

That said, one major feedback I got, was that it was annoying to always already see the solutions to the exercises. It apparently prevented people from sitting down and thinking the exercise through, before evaluating whether they got it right. As I might want to come back to using this book and as others will probably have a similar problem, I spent the past week looking around for a way to get all ```{solution} code chunks foldable and folded by default. And here it is:

One can wrap all the respective code chunks in <details></details> html tags. To automated this across all documents, I use the suggestion for such <details> elements from the RMarkdown cookbook, and get the respective code snippet to be appended to every document in the book by using the html_document includes: tag after_body:. This works, when compiling the book locally with Rscript -e 'bookdown::render_book("index.rmd")' with knitr_1.42 and bookdown_0.32.

A more thorough solution would probably change the solution chunk engine in bookdown to include a configuration option for the generic code folding functionality in RMarkdown html_document rendering. But I couldn't figure out the respective Lua code. And this solution here works, with minimal changes.

dlaehnemann avatar Mar 06 '23 12:03 dlaehnemann