After rmarkdown 1.15
This has been implemented (see related issue, PR and NEWS.md). However you should note that this folds only the code and not the output. You need to add some extra config to hide the code by default and not evaluate it.
---
title: "Bohemian Rhapcodey"
output:
html_document:
code_folding: hide
---
## Question 1
Are you in love with your car?
```{r class.source = NULL, eval = FALSE}
summary(cars)
```
## Question 2
Are you under pressure?
```{r class.source = NULL, eval = FALSE}
plot(pressure)
```
Try the knitted HTML on JSFiddle
Before rmarkdown 1.15
The issue was closed on july 2019 on GitHub. A workaround using the details element in html was suggested.
This can work for some use cases until it is actually implemented.
---
title: "Bohemian Rhapcodey"
output: html_document
---
## Question 1
Are you in love with your car?
<details>
<summary>Toggle answer</summary>
```{r cars}
summary(cars)
```
</details>
## Question 2
Are you under pressure?
<details>
<summary>Toggle answer</summary>
```{r pressure}
plot(pressure)
```
</details>
Try the knitted HTML on JSFiddle
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…