如何在Quarto中通过按钮隐藏图表?
在Quarto中实现图表的按钮式隐藏功能
Quarto本身没有提供类似code-fold: true的全局参数来一键隐藏所有图表,但可以通过以下两种方法实现单个图表的按钮式折叠:
方法一:使用原生HTML标签
通过HTML的<details>和<summary>标签可以快速实现图表的折叠效果,无需额外配置:
--- title: "How to hide graph by button" format: html: code-fold: true engine: knitr ---
# 先定义图表对象 library(ggplot2) p <- ggplot(mtcars, aes(x = qsec, y = mpg)) + geom_point()
点击显示/隐藏图表
# 输出图表 print(p)
方法二:使用Quarto折叠面板组件
用Quarto原生的面板语法,风格更统一,支持自定义标题:
--- title: "How to hide graph by button" format: html: code-fold: true engine: knitr ---
library(ggplot2)
::: panel collapse="true" title="显示/隐藏图表"
ggplot(mtcars, aes(x = qsec, y = mpg)) + geom_point()
:::
补充说明
- 两种方法都支持点击按钮切换图表的显示/隐藏状态
- 若需要默认展开图表:HTML方法给
<details>标签添加open属性;面板方法将collapse="true"改为collapse="false" - 目前Quarto无全局配置图表折叠的参数,需为每个目标图表手动添加上述结构
内容的提问来源于stack exchange,提问作者Quinten
相关产品推荐
相关产品推荐

