如何在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
相关产品推荐
相关产品推荐

