7.5 Fold all code blocks but show some initially

If code blocks in the output document are potentially distracting to readers, you may choose to fold them initially. Readers can then choose to display them by clicking the fold buttons:

  1. output:
  2. html_document:
  3. code_folding: hide

You can also choose to unfold all code blocks initially (so readers can choose to fold them later):

  1. output:
  2. html_document:
  3. code_folding: show

If you fold all code blocks initially, you can specify certain blocks to be unfolded initially with the chunk option class.source = "fold-show", e.g.,

  1. ---
  2. title: Hide all code blocks and show some initially
  3. output:
  4. html_document:
  5. code_folding: hide
  6. ---
  7. ```{r}
  8. 1 # code is hidden initially
  9. ```
  10. ```{r class.source = 'fold-show'}
  11. 2 # code is shown initially
  12. ```
  13. ```{r}
  14. 3 # also hidden
  15. ```

You can also do it the other way around, i.e., show all code blocks but hide some of them initially. For example:

  1. ---
  2. output:
  3. html_document:
  4. code_folding: show
  5. ---
  6. ```{r}
  7. 1 # code is shown initially
  8. ```
  9. ```{r class.source = 'fold-hide'}
  10. 2 # code is hidden initially
  11. ```