r - 如何在 Quarto 中用按钮隐藏图表?

标签 r button show-hide quarto

我想知道是否可以使用按钮隐藏图表,就像您可以使用 code-fold: true 使用代码块来隐藏图表一样。图表有类似的方法吗?这是一个可重现的例子:

---
title: "How to hide graph by button"
format: 
  html:
    code-fold: true
engine: knitr
---

Example code:

```{r}
library(ggplot2)
ggplot(mtcars, aes(x = qsec, y = mpg)) + geom_point()
```

输出:

enter image description here

如您所见,您可以使用名为“代码”的按钮来隐藏代码块。有没有办法只对 Quarto 中的图形执行此操作?

最佳答案

使用一点 javascript 我们可以在 Quarto 中实现这一点。

---
title: "How to hide graph by button"
format: 
  html:
    code-fold: true
    include-after-body: graph_fold.html
engine: knitr
---

Example code:

```{r}
library(ggplot2)

ggplot(mtcars, aes(x = qsec, y = mpg)) + geom_point()

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point()

print("Its not a graph")
```

graph_fold.html

<script>
function graph_fold() {
    cells = document.querySelectorAll(".cell:has(img)");
    cells.forEach(cell => {
      imgs = cell.querySelectorAll(".cell-output-display:has(p img)");
      imgs.forEach(img => {
        det = document.createElement("details");
        sum = document.createElement("summary");
        det.appendChild(sum);
        det.children[0].innerText = "Graph";
        img_clone = img.cloneNode(true);
        det.appendChild(img_clone);
        cell.replaceChild(det, img);
      });
    });
}

window.onload = graph_fold();  
</script>


graph folding button


code folding button and graph folding button works similarly


关于r - 如何在 Quarto 中用按钮隐藏图表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74089194/

相关文章:

r - 加快查找过程

java - 为什么可以编译,但不会改变 seticon

ios - 具有两个类的按钮上的 swift IBAction

jQuery 显示/隐藏在 IE 上不起作用

r - Extrafont和ggsave:字符最终在另一个字符之上

r - 考虑 GLMM 中的时间相关性

r - 使用 coefplot() 的预测变量子集

ios - 从 GameViewController 到 GameScene 访问按钮、标签和 uiimageview

excel - 如何使用按钮隐藏/取消隐藏列?

jquery - 滚动到 div 底部/从 div 底部滚动时显示/隐藏页眉页脚