Tormey R
Tormey R

Reputation: 33

Collapse RMarkdown code but display chunk label/description?

I'm using RMarkdown's code-folding to hide code in an HTML document with this in the YAML header:

---
output: 
  html_document:
    code_folding: hide
---

But in the rendered document it's impossible to know what each chunk is doing without unfolding it, or putting a text description before/after it.

Is there a way to change the appearance of a folded chunk so it displays the chunk label (or a short description) of each chunk next to the "Show" button, preferably in a way that distinguishes it from regular markdown text? Ideally I'm hoping to have a chunk like this:

#| fold-label: "Import and cleanup data"
#| warning: FALSE

library(tidyverse)

mydata <- mtcars %>%
  mutate(cleaned = T)

Look like this when folded (but keeping the Show button):

# Import and cleanup data

Upvotes: 0

Views: 50

Answers (1)

margusl
margusl

Reputation: 17389

If you have an option to switch from RMarkdown to Quarto, you could use code-summary.

tmp.qmd :
---
format:
  html:
    code-fold: true
---
```{r}
#| code-summary: "Import and cleanup data"
mtcars
```

Renders as: QMD render with folded code block and code-summary

Upvotes: 1

Related Questions