I'm using quarto to generate an html report which contains a large number of plots. I don't want all of these plots to display, but I want the end-user to be able to display them if desired.
Quarto can have foldable code, but not foldable output, so I have found something like a workaround in html by saving the plot to a png, and using a collapsible button.
---
title: "untitled"
format: html
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE)
library(tidyverse)
library(glue)
```
```{r}
a <- c(1,2,3)
b <- c(13, 16, 33)
c <- as.data.frame(cbind(a,b))
PLOT <- ggplot(c, aes(x = a, y = b)) +
geom_point() +
labs(title = "Title")
ggsave(plot = PLOT, file = paste("PLOT.png"))
```
```{=html}
<button class="btn btn-link" type="button" data-bs-toggle="collapse" data-bs-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
Expand me
</button>
<div class="collapse" id="collapseExample">
<div class="card card-body">
<img src="PLOT.png">
</div>
</div>
```
Ideally, I would be able to turn this into a function and either refer to the R plot object directly, or refer to the *.png. The following generates the right html code, but doesn't deploy it.
Would be very grateful if someone can help me to functionise this code, as I have hundreds of graphs, and copy-pasting all that html and renaming the buttons leaves a lot of room for error.
```{r}
f_hide_plot <- function(plot_output) {
glue::glue('<button class="btn btn-link" type="button" data-bs-toggle="collapse" data-bs-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
Expand me
</button>
<div class="collapse" id="collapseExample">
<div class="card card-body">
<img src="{plot_output}.png">
</div>
</div>')
}
```
```{r}
glue::glue('{f_hide_plot(plot_output = "PLOT")}')
```