2

I'm producing an RMarkdown document with knitr, and I'd like to give my readers the option to show/hide code blocks.

I assume that this will involve adding some JavaScript (e.g. toggle_visibility), but I'm not sure how to add it to a knitr document. Would I add it to the CSS somehow? Or return something with asis = TRUE and put the JavaScript there, as in this example?

Thanks in advance for your help.

David J. Harris
  • 1,372
  • 1
  • 8
  • 13

2 Answers2

2

You can include additional content in the HTML header, before body, or after body for HTML output from R Markdown documents. I have a simple JavaScript function here, and you may include it in <script></script>. The knitrBootstrap package is much fancier than my naive JS function.

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
1

In addition to @Yihui's answer and the one @Shaun Jackman referenced in a comment, someone also pointed me to this example:

Edited to add a minimal example. Note that this example requires jquery.

David J. Harris
  • 1,372
  • 1
  • 8
  • 13