Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collapsible Code Output with <details> #219

Open
yihui opened this issue Jul 24, 2019 · 3 comments
Open

Collapsible Code Output with <details> #219

yihui opened this issue Jul 24, 2019 · 3 comments

Comments

@yihui
Copy link
Owner

yihui commented Jul 24, 2019

By @emitanaka: https://twitter.com/statsgen/status/1152440358124437505
https://gist.github.com/emitanaka/eaa258bb8471c041797ff377704c8505

I'd like to use one function instead of two since the two look so similar to each other:

collapse-output.html

<script>
(function() {
  var divHTML = document.querySelectorAll(".details");
  divHTML.forEach(function (el) {
    var preNodes = el.getElementsByTagName("pre");
    if (preNodes.length < 2) return;
    var outputNode = preNodes[1];
    outputNode.outerHTML = "<details " +
      (el.classList.contains("open") ? "open " : "") +
      "class='output'><summary>Output</summary>" +
      outputNode.outerHTML + "</details>";
  })
})();
</script>

test.Rmd

---
output:
  xaringan::moon_reader:
    includes:
      after_body: "collapse-output.html"
---

### This output is closed first

.details[
```{r}
summary(cars)
```
]

### This output is open first

.details.open[
```{r}
str(cars)
```
]

Possible improvements

One .details[] can contain multiple output blocks, e.g.,

.details.open[
```{r}
1:50
str(cars)
```
]

In this case, we don't only process preNodes[1] but all <pre> tags for outputs. To differentiate output <pre> from source <pre>, we have to look at its <code> child and check if its el.classList.length > 1 (source <pre>'s <code> child have multiple classes).

Another idea is to use knitr output hooks for an alternative syntax (add .details[] automatically in the output hook):

```{r, output.details = NULL (default) / TRUE (open) / FALSE (closed)}
1:50
str(cars)
```

https://yihui.name/knitr/hooks/

Perhaps this is not worth the effort since it doesn't save much typing effort.

I'm just writing down the ideas here because this might be an interesting topic for the xaringan book. Of course, we can also just support this feature in this package.

@emitanaka
Copy link
Sponsor Collaborator

I love how concise you were able to make my JS.
I didn't realise that you can customise the output via output hooks. That will make things cleaner.
Adding the feature directly in the package is welcomed :) I think it may be useful for general Rmd and not just xaringan.
Speaking of the xaringan book, someone should get her writing going 😅

@jvcasillas
Copy link
Contributor

Speaking of the xaringan book, someone should get her writing going 😅

That makes two of us. I have a long flight to Australia Friday so I’m going to get back on track. And to make this post somewhat relevant, I think the collapsible code output is a great feature. 👍🏽

@emitanaka
Copy link
Sponsor Collaborator

@jvcasillas where in Australia are you coming? I'm in Sydney.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants