我想知道是否可以使用按鈕隱藏圖形,就像您可以使用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()
```
輸出:

如您所見,您可以使用一個名為“代碼”的按鈕來隱藏代碼塊。有沒有辦法只對中的圖表執行此操作Quarto?
uj5u.com熱心網友回復:
使用一點 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>


轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/518088.html
標籤:r按钮显示隐藏四开
上一篇:將小部件或回呼傳遞給按鈕
