31

Is there a way to have code folding available for individual chunks in an R Markdown document, but not others (without writing customized JavaScript)?

I know I can use the code_folding YAML option, but that applies to the whole document. I'd like to enable it for individual chunks, but not all chunks.

[The reason is for writing a lab that contains instructions that should not be hideable, but with questions that have show/hide solutions.]

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
beanumber
  • 767
  • 5
  • 13
  • 10
    It is an open feature request, so probably there isn't a way yet: https://github.com/rstudio/rmarkdown/issues/664 – Frank Mar 01 '17 at 22:52

4 Answers4

7

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

7hibault
  • 2,371
  • 3
  • 23
  • 33
  • 5
    OP wants code folding for _individual_ chunks, they are already aware of the `code_folding` yaml option – Hong Ooi Oct 10 '19 at 12:39
  • @HongOoi Please re-read the answer and check the provided links and the knitted html if you are not convinced. The code_folding option now also applies to individual chunks. OP was aware of the `code_folding` option in 2017 when individual code folding was not yet available, future readers might find it useful to know that it is now officially supported. – 7hibault Oct 10 '19 at 12:41
  • I believe the `class.source` argument in the `knitr` cells should be equal to `"fold-show"` as described in [bookdown](https://bookdown.org/yihui/rmarkdown-cookbook/fold-show.html) as opposed to equal to NULL – Luis Chaves Rodriguez Nov 07 '20 at 09:25
3

With rmarkdown version 2.3

Based on 7hibaut's answer, but not quite the same. Use the option class.source = "fold-show" in the chunk header to show a chunk, when other chunks have been hidden with the code_folding: hide in the YAML, as described on the rmarkdown pull request.

    ---
    title: "Bohemian Rhapsody"
    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)
    ```

    ## Question 3

    suggested code

    ```{r class.source = "fold-show", eval = FALSE}
    library(dplyr)
    library(magrittr)
 
    a <- dMeasure::dMeasure$new()
    a$open_emr_db()
    ## active patients
    kensington_clinicians <- a$UserConfig %>>%
      filter(
        grepl(
          "Kensington",
          purrr::map_chr(Location, function(x) paste(x, collapse = ", "))
          # map_chr will create a 'collapsed' version of all the
          # listed locations
        )
    )
    ```

enter image description here

David Fong
  • 506
  • 4
  • 3
2

The workaround using the details element works well, but to improve discoverability I would recommend adding some code so that the user sees a pointing hand when s/he hovers over the element. This makes it more obvious that the element is interactive. Expanding on 7hibault's example:

<style type="text/css">
details:hover { cursor: pointer }
</style>

---
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>
user2363777
  • 947
  • 8
  • 18
0

Here's what I do when code_folding: hide and you want to display select code chunks:

```
5 * 5
```
```{r}
5 * 5 
```

The first block (without the {r}) simply displays the code, and the second block runs the code

stevec
  • 41,291
  • 27
  • 223
  • 311