How can I decouple code and output within a single chunk in xaringan?

78 views Asked by At

Whenever I put together a slide deck in xaringan, I find myself often doing this:

```{r addition, eval = FALSE}
2 + 3
--
```{r addition-out, ref.label = "addition", echo = FALSE}

This allows a moment to discuss to code and ask students what they expect it to give before going ahead and showing the result.

It's long irked me how this muddies up the source, so I'm coming around to thinking about if there's a better solution. I was wondering if anyone has an opinion on:

  1. If there is a better way to do this that currently exists.
  2. If there would be new approach that would be easy enough to implement.

From a functionality standpoint, one approach would be to create an progressive.eval knitr chunk option that could be toggled to TRUE so that it would take a single R chunk, split it based on print/plot statements, then split those into an echoed and an evaluated chunk. Another more general approach would be to do something similar to knitr::fig_chunk() and capture any print/plot output from a chunk and automatically label them <chunk-label>-out-1, <chunk-label>-out-2, etc., then make those available to be put elsewhere in the doc. Essentially soft references to any chunk

Does this seem like a sensible approach? Does anyone have a pointer for where to start if I were to try to implement this myself?

0

There are 0 answers