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
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
How to Add an Inset (Subplot) to "Topright" of an R Plot
Fastest Way for Filling-In Missing Dates for Data.Table
How to Draw Gridlines Using Abline() That Are Behind the Data
Check If Each Row of a Data Frame Is Contained in Another Data Frame
How to Create 3D - Matlab Style - Surface Plots in R
Ggplot2 Increase Space Between Legend Keys
How to Automate Multiple Requests to a Web Search Form Using R
How to Exit a Shiny App and Return a Value
Export Each Data Frame Within a List to CSV
Multiple Boxplots Using Ggplot
Subtracting Values Group-Wise by the Average of Each Group in R
Can't Change Fonts in Ggplot/Geom_Text
How to Annotate() Ggplot with Latex
R Install Package Loaded Namespace
Create Unique Identifier from the Interchangeable Combination of Two Variables
What Are 'User' and 'System' Times Measuring in R System.Time(Exp) Output