I am knitting to HTML. When scrolling through the report, I would like to be able to show/hide a plot via pressing an onscreen toggle. The way I have been doing this is by using HTML code as a sandwich around my R chunk where I call the plot.
This has worked but in one case, I would like to only create the HTML toggle if a certain condition is met (r condition_met <- TRUE
). This was my code attempt which failed because there is a #
inside the asis_out("")
function.
How else can I output the HTML code within the R chunk or make the plot 'toggle-able'?
`r condition_met <- TRUE`
```{r, eval = TRUE}
if(condition_met==TRUE){
asis_out("<button class="btn btn-primary" data-toggle="collapse" data-target="#button_name">\\n")
asis_out("Show/Hide Plot\\n")
asis_out("</button>\\n")
asis_out("::: {#button_name .collapse.show}\\n")
plot(cars)
asis_out(":::\\n")
}
```
EDIT:
Another option I tried was the code below after following https://stackoverflow.com/a/35596636/8076560 and https://stackoverflow.com/a/34641628/8076560. Alas, this doesnt work either. Even a print.ASIS
doesnt work either. Instead, I keep having the #
from the HTML code throwing an error.
```{r, eval = TRUE, results = "asis"}
cat("<button class="btn btn-primary" data-toggle="collapse" data-target="#button_name">\\n")
cat("Show/Hide Plot\\n")
cat("</button>\\n")
cat("::: {#button_name .collapse.show}\\n")
plot(cars)
cat(":::\\n")
```
EDIT2:
Unfortunately, this setup doesnt work either:
```{asis, eval = TRUE}
<button class="btn btn-primary" data-toggle="collapse" data-target="#button_name">
Show/Hide Plot
</button>
::: {#button_name .collapse.show}
```
```{r, eval = TRUE}
plot(cars)
```
```{asis, eval = TRUE}
:::
```