The Code folding option in RMarkdown for html documents is awesome. That option makes the programmatic methodology transparent for those who are interested, without forcing the audience to scroll through miles of code. The tight placement of code with prose and interactive graphical output makes the whole project more accessible to a wider audience, and furthermore it reduces the need for additional documentation.
For a larger project, I'm using bookdown, and it works great. The only problem is that there is no code-folding option. Code folding is not currently enabled in bookdown. (see Enable code folding in bookdown )
I know I don't need an option to make it happen. I just need to paste the right code in the right place or places. But what code and where?
A viable alternative would be to put the code chunk below the chunk's outputs in the page. Or, finally, to put them as an appendix. I could do that with html but not reproducible like rbookdown.
Global Hide/Show button for the entire page
To use @Yihui's hint for a button that fold all code in the html output, you need to paste the following code in an external file (I named it
header.html
here):Edit: I modified function
toggle_R
so that the button showsHide Global
orShow Global
when clicking on it.In this script, you are able to modify the position and css code associated to the button directly with the
style
options or add it in your css file. I had to set thez-index
at a high value to be sure it appears over other divisions.Note that this javascript code only fold R code called with
echo=TRUE
, which is attributed aclass="r"
in the html. This is defined by commandvar x = document.getElementsByClassName('r');
Then, you call this file in the YAML header of your rmarkdown script, as in the example below:
New Edit: Local Hide/show button for each chunk
I finally found the solution !
While looking at the code folding behavior for normal html output (no bookdown), I was able to add it to bookdown. The main javascript function needs to find
.sourceCode
class divisions to work with bookdown. However, this also requires complementary javascript functions of bootstrap, but not all. This works withgitbook
andhtml_document2
.Here are the steps:
js
folder in the same directory than your Rmd filetransition.js
andcollapse.js
here for instance: https://github.com/twbs/bootstrap/tree/v3.3.7/js and store them in yourjs
folderjs
folder calledcodefolding.js
with the following code. This is the same as for rmarkdown code_folding option but withpre.sourceCode
added to find R code chunks:codefolding.js
code:js
folder in not useful for the final document itself. When reading the js functions, I also added the option toshow
code blocks by default, but you can choose to hide them withhide
.rmarkdown code:
This script shows the buttons in the Rstudio browser but does not work well. However, this is ok with firefox.
You'll see that there is a little
css
in this code, but of course you can modify the position and color and whatever you want on these buttons with some more css.Edit: Combine Global and local buttons
Edit 2017-11-13: Global code-folding button well integrated with individual bloc buttons. Function
toggle_R
is finally not necessary, but you need to get functiondropdown.js
in bootstrap.Global button is called directly in the code chunk when calling
js
files:The new global button shows a dropdown menu to choose between "show all code" or "hide all code". Using
window.initializeCodeFolding("show" === "show")
all codes are shown by default, whereas usingwindow.initializeCodeFolding("show" === "hide")
, all codes are hidden by default.