在编织R标记文件的过程中,以编程方式将标记添加到yaml头中



我想通过编程将标记添加到blogdown文章的YAML标题中;针织";按钮

例如:

---
title: This is a post
author: brshallo
date: '2022-02-11'
tags: "`r stringr::str_c('n  - ', stringr::str_flatten(c('rstats', 'datascience'), 'n  - '))`"
slug: this-is-a-post
---

相关资源

我用CCD_ 1、CCD_,以及这些方面的迭代,但到目前为止运气不佳。

以下是一些相关的线程:

  • 在rstudio/knitr文档中导入通用YAML
  • 如何以编程方式编辑R markdown YAML标头
  • https://community.rstudio.com/t/is-there-a-way-to-specify-a-nested-group-of-params-in-rmarkdown-yaml-if-not-should-there-be/117393/2
  • https://community.rstudio.com/t/rmarkdown-list-in-yaml/94147/5
  • 包:ymlthis和yaml
  • 上下文:blogdown#647

我起初认为这是一个更普遍的rmarkdown问题,而不是特定于blogdown的问题,但考虑到上面线程中提到的一些解决方案在输入通用参数时似乎有效,我不确定。。。

要生成有效的YAML数组,可以使用替代语法[],例如

tags: ["`r paste(head(letters), collapse = '", "')`"]

生成:

tags: ["a", "b", "c", "d", "e", "f"]

注意破解collapse = '", "':由于R表达式外已经存在一对双引号,因此应该只从R表达式生成部分a", "b", "c", "d", "e", "f

--解决方案复制自Yihui在博客#647上的解释

最新更新