You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Quarto图子标题中添加R行内代码?

解决Quarto图子标题中R表达式不生效的问题

问题出在Quarto对YAML列表项中!expr的解析逻辑上——当fig-subcap以列表形式定义时,单个列表项前的!expr无法被正确识别执行。正确的做法是将整个fig-subcap的值设置为R表达式,返回对应长度的字符向量。

修改后的可运行代码如下:

test = 403.87
#| label: example
#| code-fold: true
#| fig-show: "hold"
#| layout-ncol: 2
#| fig-cap: !expr "paste0('R expression in the caption works: ', test)"
#| fig-subcap: !expr c(test, paste0('R expression in the subcaption works now: ', test))

library(ggplot2)
ggplot(airquality, aes(Temp, Ozone)) + 
        geom_point() + 
        geom_smooth(method = "loess", se = FALSE)
ggplot(airquality, aes(Temp, Ozone)) + 
        geom_point() + 
        geom_smooth(method = "lm", se = FALSE)

说明

将fig-subcap的取值改为!expr c(...),让Quarto执行这段R代码生成包含两个子标题的字符向量,这样就能正常渲染动态生成的子标题内容了。

内容的提问来源于stack exchange,提问作者ramiro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 03:54:52