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.]
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
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
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>
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
)
)
```
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