1

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}
    :::
```
Martin
  • 401
  • 6
  • 15
  • 1
    what package does `asis_out` come from? i get very different results depending on whether your asis/plot are in a conditional vs if they are not (even if the conditional is always true), i'm not sure how to explain that. anyway, would it would if you put `eval=condition_met` and removed the conditional from the code chunk entirely? – rawr Nov 12 '20 at 19:44
  • It seems that `asis` is an engine according to answers here: https://stackoverflow.com/a/34641628/8076560. The original answer (and related question) I was following was here: https://stackoverflow.com/a/35596636/8076560 – Martin Nov 12 '20 at 22:09

0 Answers0