如何将缺少的 YAML header 信息添加到脚本中的 .rmd R Markdown 文件?

How do I add missing YAML header info to .rmd R Markdown file within a script?

我正在开发一个工作流程,我

  1. 协作处理 Google 文档,然后
  2. 使用 googledrive 包下载为 .docx,
  3. 使用 rmarkdown:pandoc_convert()、
  4. 转换为 .rmd
  5. 应用了样式和徽标并且
  6. 呈现为 .html & PDF 以供分发。

我目前在第 3 步挂断了,当时 .rmd 文件没有 header。

 pandoc_convert("example.docx", "markdown", output = "out.Rmd")

如何从工作流脚本中的另一个文件注入 YAML? 例如这个 header:

---
  title: "Title1"    
  html_document:
  number_sections: yes
  self_contained: yes
  toc: yes
  toc_depth: 3
  toc_float: yes
---

假设您的 header 在 header.yaml 中。然后简单地读取这两个文件并将它们作为一个文件写出来:

fulltext <- c(readLines("header.yaml"), readLines("out.Rmd"))
writeLines(fullText, "out2.Rmd")

当然,您也可以将 header 放入字符串变量中,而不是从文件中读取它,例如

header <- '---
  title: "Title1"    
  html_document:
  number_sections: yes
  self_contained: yes
  toc: yes
  toc_depth: 3
  toc_float: yes
---'
fulltext <- c(header, readLines("out.Rmd"))
writeLines(fullText, "out2.Rmd")