Collapsing/Hiding Figures in R Markdown

Collapsing / hiding figures in R markdown

If you add this to the end of your .Rmd file

<script>
$( "input.hideshow" ).each( function ( index, button ) {
button.value = 'Hide Output';
$( button ).click( function () {
var target = this.nextSibling ? this : this.parentNode;
target = target.nextSibling.nextSibling.nextSibling.nextSibling;
if ( target.style.display == 'block' || target.style.display == '' ) {
target.style.display = 'none';
this.value = 'Show Output';
} else {
target.style.display = 'block';
this.value = 'Hide Output';
}
} );
} );
</script>

and then this before each chunk you want to have a toggle:

<input type=button class=hideshow></input>

(adapted from here: https://groups.google.com/forum/#!topic/knitr/d37E0mP3w6k)

Note: this will work if you show the code - if you are hiding the code (with echo = FALSE), change

target = target.nextSibling.nextSibling.nextSibling.nextSibling;

to

target = target.nextSibling.nextSibling;

Note 2: if you want to use the code_folding option, change

 target = target.nextSibling.nextSibling.nextSibling.nextSibling;

to

 target = target.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling;

RMarkdown collapsible panel

Another simple solution that would work (but without buttons and styling).

```{r, eval=FALSE}
hist(1:10)
```

<details>
<summary>Click for Answer</summary>
```{r, echo=FALSE, eval=TRUE}
hist(1:10)
```
</details>

And here are the two states:

Collapsed
collapsed

Expanded
expanded

Collapsing minor levels of floating table of contents in rmarkdown html document

Well, I've had a go and got something to work by adding a small javascript function to the bottom of the RMarkdown file. Whether this fits the use case I'm not sure! This is the full contents of the .Rmd file:

Edit

Extra button added to collapse level 3 headings only in response to comments

---
output:
html_document:
toc: true
toc_float:
collapsed: true
---

# Chapter 1

## Section 1-1

### Figure 1-1-1

### Figure 1-1-2

# Chapter 2

## Section 2-1

### Figure 2-1-1

### Figure 2-1-2

---

Select the menu on the left to expand / collapse table of contents (TOC)
entries. Press button below to collapse all TOC except the top level headings.

<button id="btnCollapseHeading" onclick="collapseTOC()">Collapse sub-headings</button>

If you only want to collapse level 3 headings press this button.

<button id="btnCollapseLevel3" onclick="collapseLevel3()">Collapse Level 3 only</button>

<script>
function collapseTOC() {
var x = document.getElementsByClassName("tocify-subheader");
var i;
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
}

function collapseLevel3() {
var x = document.getElementsByClassName("tocify-subheader");
var i;
for (i = 0; i < x.length; i++) {
if (x[i].getAttribute("data-tag") == "3") {
x[i].style.display = "none";
}
}
}

</script>

I've also added an example to try out at https://github.com/markdly/rmd-toc-button

Hiding the R code in Rmarkdown/knit and just showing the results

Sure, just do

```{r someVar, echo=FALSE}
someVariable
```

to show some (previously computed) variable someVariable. Or run code that prints etc pp.

So for plotting, I have eg

### Impact of choice of ....
```{r somePlot, echo=FALSE}
plotResults(Res, Grid, "some text", "some more text")
```

where the plotting function plotResults is from a local package.

How to hide code in RMarkdown, with option to see it

This has been made much easier with the rmarkdown package, which did not exist three years ago. Basically you just turn on "code folding": https://bookdown.org/yihui/rmarkdown/html-document.html#code-folding. You no longer have to write any JavaScript.

E.g.

---
title: "Habits"
output:
html_document:
code_folding: hide
---

Also see https://bookdown.org/yihui/rmarkdown-cookbook/fold-show.html for more control over which code blocks to be fold or unfold.



Related Topics



Leave a reply



Submit