I am writing up a lesson in HTML using rmarkdown to demonstrate how to implement analytic methods in R, and because of this the document has a lot of code that is needed to understand those methods, but also a lot of code that is used only for generating plots and figures. I would like to show the first sort of code by default, and leave the plotting code available for students to view but hidden by default.
I know that rmarkdown has recently added support for code folding by setting the code_folding html_document argument to either show or hide. However, this either leaves all code chunks unfolded or folded by default -- is there any way to indicate whether individual code chunks should be shown or folded by default while allowing code folding?
Thank you!
Code folding Rmarkdown offers the option to interactively collapse the code chunks in a knitted document. This may not be an option to have in a final report, but it may prove useful for a technical document where both code and output are to be shared.
Code folding allows you to easily show and hide blocks of code to make it easier to navigate your source file and focus on the coding task at hand.
Code chunks in an R Markdown document contain your R code. All code chunks start and end with ``` – three backticks or graves.
I arrived here wondering the same thing. This is a not a perfect solution, but I write the code twice: once in regular markdown (so it displays - note no {r} after the three backticks), and another time in a code chunk (so it runs).
Example:
This runs but doesn't display the actual code
```{r}
5 * 5
```
This results in both the code and execution being displayed
```
5 * 5
```
```{r}
5 * 5
```
Which results in:

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With